通过线程通过FTP上传大文件 [英] Uploading Large Files via FTP by Threading
问题描述
大家好!
我将大文件上传到FTP时遇到问题。我正在尝试创建一个新线程并以这种方式上传。
这是我到目前为止:
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.Invoke(新的UpdateProgessCallback(this.UpdateProgress), new object [] {reqStream.Length,stream.Length});
出现此错误:
此流不支持搜索操作。
之前有没有人见过这个错误?如果是这样,你是如何解决它的。我无法通过谷歌找到解决方案。
谢谢大家!
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!
推荐答案
我自己解决了。这是我通过FTP查询确定提交成功或失败的文件的方式。
I solved it myself. It was the way I was FTP'ing the file that determined the success or failure of the submission.
这篇关于通过线程通过FTP上传大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!