Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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

1 solution

I probably would have changed this

C#
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

C#
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);
 
Share this answer
 
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)



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