Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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:
 
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 2-Jul-13 6:24am
Comments
Prasad Khandekar at 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 at 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

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

Solution 1

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

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

  Print Answers RSS
0 OriginalGriff 525
1 Maciej Los 319
2 BillWoodruff 174
3 Suraj Sahoo | Coding Passion 170
4 /\jmot 152
0 OriginalGriff 8,654
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,229
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 4 Feb 2014
Copyright © CodeProject, 1999-2014
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