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 have an issue with uploading large files to FTP. I'm trying to create a new thread and upload it that way.

Here is what I have so far:

C#
private void uploadButton_Click(object sender, EventArgs e)
{
     if (ofd_psUpdate.ShowDialog() == DialogResult.OK)
     {
          thread = new Thread(UploadFile);
          thread.Start();
     }
}

private void UploadFile(object obj)
{
     try
     {                
          FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create("ftpURL" + ofd_psUpdate.SafeFileName);
          request.Method = WebRequestMethods.Ftp.UploadFile;
          request.Credentials = new NetworkCredential(ftpUserNameTextBox.Text.Trim(), ftpPasswordTextBox.Text.Trim());
          request.UsePassive = true;
          request.UseBinary = true;
          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[] { reqStream.Length, stream.Length });
          }                
     }
     catch (Exception ex)
     {
          MessageBox.Show("Fail\n\n" + ex.Message);
     }
     finally
     {
          //reqStream.Close();
          //stream.Close();
     }
}

private void UpdateProgress(Int64 BytesRead, Int64 TotalBytes)
{
     // Calculate the download progress in percentages
     PercentProgress = Convert.ToInt32((BytesRead * 100) / TotalBytes);
     // Make progress on the progress bar
     progressBar1.Value = PercentProgress;
     // Display the current progress on the form
     label20.Text = "Uploaded " + BytesRead + " of " + TotalBytes + " (" + PercentProgress + "%)";
}


This is supposed to upload a file and show the progress on a progress bar and update a label with the progress, too.

The code is failing on this line:
this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { reqStream.Length, stream.Length });
with this error:
This stream does not support seek operations.

Has anyone seen this error before? If so, how did you resolve it. I've not been able to find a resolution for this via google.

Thanks everyone!
Posted
Comments
Prasad Khandekar 2-Jul-13 11:42am    
You probably don't need to use reqStream.Length and stream.Length use local variables instead. You already know the bytes read , accumulate those and you can use FileInfo.Length to find the file size. Something like one shown below.

FileInfo fi1 = new FileInfo(ofd_psUpdate.FileName);
int totRead = 0;
int fLen = fil.Length;
while ((bytesSize = stream.Read(buffer, 0, buffer.Length)) > 0)
{
totRead += bytesSize;
reqStream.Write(buffer, 0, bytesSize);
this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] {totRead, fLen});
}


Regards,
joshrduncan2012 2-Jul-13 11:47am    
Thanks Prasad! I've tried a version of what you are showing. It seems to work. The file I'm trying to upload is >50MB in size and I'm a little worried that how quick it's completing may not be correct.

1 solution

I solved it myself. It was the way I was FTP'ing the file that determined the success or failure of the submission.
 
Share this answer
 

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