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
{
}
}
private void UpdateProgress(Int64 BytesRead, Int64 TotalBytes)
{
PercentProgress = Convert.ToInt32((BytesRead * 100) / TotalBytes);
progressBar1.Value = PercentProgress;
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!