Click here to Skip to main content
15,910,872 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
C#
protected void btnsave_Click(object sender, EventArgs e)
{
    using (SqlConnection condatabase = new SqlConnection(constr))
    {
        string FileSaveUri = @"ftp://111.111.111.111/httpdocs/uploadpic/";  
        
        string ftpUser = "sa";
        string ftpPassWord = "111";  
        Stream requestStream = null;  
        Stream fileStream = null;  
        FtpWebResponse uploadResponse = null;
        
        if (FileUpload.PostedFile != null)
        {
            HttpPostedFile uploadFile = FileUpload.PostedFile;
            if (System.IO.Path.GetExtension(uploadFile.FileName).ToLower() == ".jpg")
            {
                string strFileName = Path.GetFileName(uploadFile.FileName);
                string a = ("../musicnote123/uploadpic/" + strFileName);
          
                int FileLength = FileUpload.PostedFile.ContentLength;
              
                if (FileLength < 512 * 1024 * 1024)
                {
                    try
                    {
                        
                        Uri uri = new Uri(FileSaveUri + Path.GetFileName(FileUpload.PostedFile.FileName));
                        FtpWebRequest uploadRequest = (FtpWebRequest)WebRequest.Create(uri);
                        uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;
                        uploadRequest.Credentials = new NetworkCredential(ftpUser, ftpPassWord);  
                        requestStream = uploadRequest.GetRequestStream();
                        byte[] buffer = new byte[FileLength];
                        fileStream = FileUpload.PostedFile.InputStream; 
                        fileStream.Read(buffer, 0, FileLength);
                        requestStream.Write(buffer, 0, FileLength); 
                        requestStream.Close();
                        uploadResponse = (FtpWebResponse)uploadRequest.GetResponse();
        
                    uploadFile.SaveAs(MapPath(a));
        
                    SqlCommand cmdsave;
                    string sqlsave = "Update memberlist set member_pic=@mpic Where member_id='" + Convert.ToString(Session["username"]) + "'";
        
                    cmdsave = new SqlCommand(sqlsave, condatabase); 
                    cmdsave.Parameters.Add(new SqlParameter("@mpic", a));
        
                    condatabase.Open();
                    cmdsave.ExecuteNonQuery();
                    condatabase.Close();
        
                    }
                    catch (Exception ex)
                    {
          
                    }
                    finally
                    {
                        if (uploadResponse != null)
                            uploadResponse.Close();
                        if (fileStream != null)
                            fileStream.Close();
                        if (requestStream != null)
                            requestStream.Close();
                    }
                }
                else {}
            }
            else {}            
        else {}
    }     
}
Posted
Updated 14-Mar-13 20:07pm
v2
Comments
Karthik Harve 15-Mar-13 2:07am    
[Edit] pre tags added.
kangyi.lee 15-Mar-13 2:26am    
pre tags added? sry i not understand your meaning?
Karthik Harve 15-Mar-13 2:33am    
you may refer this.
Using PRE tags on Code Project[^]
aankur81 16-Mar-13 3:34am    
You Can Use Background Worker It is Very Simple

or you can do this also

Create a Thread to show Progress Bar.. and in another thread Run You code
kangyi.lee 18-Mar-13 6:48am    
haiz.... my visual studio 2005 dun have Background Worker & progress bar. can any person recommence any website to download those function??

1 solution

Add a Background Worker and your code should (or could) end up looking something like this:

C#
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            //Do NOT (ever) refer to form controls from this method
            //Keep it self contained

            //Add your background code
            //In your case it's your "btnsave_Click" method code, just check you aren't referring back to objects owned by the form thread:

            //using (SqlConnection condatabase = new SqlConnection(constr))
            //{
                //string FileSaveUri = @"ftp://111.111.111.111/httpdocs/uploadpic/";

                //string ftpUser = "sa";
                //string ftpPassWord = "111";
                //Stream requestStream = null;
                //Stream fileStream = null;
                //FtpWebResponse uploadResponse = null;

                //if (FileUpload.PostedFile != null)
                //{
                    //etc.

            //As you pass through each loop in your long runtime method do something like this
            backgroundWorker1.ReportProgress(Convert.ToInt32((countsofar / totalofthingstodo) * 100));
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            //Only ever refer to form controls from this method
            progressBar1.Value = e.ProgressPercentage;
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            progressBar1.Value = progressBar1.Minimum;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }
    }


For the above to work the BackgroundWorker must have WorkerReportsProgress set to True.

If you can't calculate the percentage progress then make the progressBar a Marquee (Style = Marquee) and don't report progress, just have it default to disabled (Enabled = False), enable it at the start of the job to be done and disable it in the backgroundWorker1_RunWorkerCompleted method.

Mike
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900