Click here to Skip to main content
12,396,047 members (66,139 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# Forms FTP VS2012
Hi everyone!

I am in the process of developing an FTP process for large file uploads (>50MB in size). I have a threading process that uploads the file to the server, but the file continues to grow until I kill the whole windows application. If I don't kill the process, it will continue to grow forever.

Here's what I'm referring to:

if (ofd_psUpdate.ShowDialog() == DialogResult.OK)
{
     Thread thread = new Thread(UploadFile);
     thread.Start();
}
 
private void UploadFile(object obj)
{
     try
     {
          uploadPicoScopeButton.Invoke
               ((MethodInvoker)(() => { uploadPicoScopeButton.BackColor = Color.Lime; } ));
 
          FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create("ftp://www.pico-usa.com/" + ofd_psUpdate.SafeFileName);
          request.Method = WebRequestMethods.Ftp.UploadFile;
          request.Credentials = new NetworkCredential(ftpUserNameTextBox.Text.Trim(), ftpPasswordTextBox.Text.Trim());
          request.UsePassive = true;
          request.UseBinary = false;
          request.KeepAlive = true;
 
          stream = File.OpenRead(ofd_psUpdate.FileName);
          byte[] buffer = new byte[stream.Length];
          int bytesSize = 0;
 
          reqStream = request.GetRequestStream();
 
          while ((bytesSize = stream.Read(buffer, 0, buffer.Length)) > 0)
          {
               reqStream.Write(buffer, 0, bytesSize);
               this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { bytesSize, stream.Length });
          }
        
          uploadPicoScopeButton.Invoke
               ((MethodInvoker)(() => { uploadPicoScopeButton.BackColor = Color.Transparent; }));
          }
          catch (Exception ex)
          {
               uploadPicoScopeButton.Invoke
                    ((MethodInvoker)(() => { uploadPicoScopeButton.BackColor = Color.Red; }));
 
               MessageBox.Show("FTP Upload Fail\n\n" + ex.Message);
                
               uploadPicoScopeButton.Invoke
                    ((MethodInvoker)(() => { uploadPicoScopeButton.BackColor = Color.Transparent; }));
          }
}
 
private void UpdateProgress(Int64 BytesRead, Int64 TotalBytes)
{
     progressLabel.BeginInvoke(
     (MethodInvoker)delegate() { progressLabel.Text = "Uploaded " + BytesRead + " of " + TotalBytes + " (" + PercentProgress + "%)";
     progressLabel.Refresh(); });
}

Can anyone tell if I'm missing something simple to prevent the file upload process from being an infinite process? My goal is to have the process stop when the amount of the file that has been uploaded equals the actual size of the file, no more no less.

Thanks everyone!
Posted 2-Jul-13 11:48am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

I probably would have changed this

byte[] buffer = new byte[stream.Length];
          int bytesSize = 0;
 
          reqStream = request.GetRequestStream();
 
          while ((bytesSize = stream.Read(buffer, 0, buffer.Length)) > 0)
          {
               reqStream.Write(buffer, 0, bytesSize);
               this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { bytesSize, stream.Length });
          }

to

byte[] buffer = new byte[1024];
          int bytesSize = 0;
 
          reqStream = request.GetRequestStream();
          
          do 
          {
               bytesSize = stream.Read(buffer,0,buffer.Length)
               reqStream.Write(buffer,0, bytesSize);
               this.Invoke(new UpdateProgressCallback(this.UpdateProgress),new object[] {bytesSize, stream.Length });
          } 
          while (bytesSize != 0);
  Permalink  
Comments
joshrduncan2012 3-Jul-13 9:16am
   
Thanks Garth! I just realized a VS keyword is misspelled that ships out with VS 2012. "UpdateProgressCallback" is not known but "UpdateProgessCallback" is accepted. (Whoops!)

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 2 Jul 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100