[英]FTP Upload string from memory with background worker and progress bar using C# and .NET4
我的问题与此和这一问题相似,但是我无法在我的应用程序中使用它们中的任何一个。 也许我缺少什么?
这是我要完成的工作:
这是我现在拥有的代码:
private void bkgd_Submit_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
string host = @"ftp://ftp.somesite.net/submissions/";
string user = "username";
string pass = "password";
string[] lines = { "First line", "Second line", "Third line" };
string ftpFile = host + "filename.txt";
//the string that needs to get uploaded
string message = "Hello this is the first order.";
FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(ftpFile);
ftpRequest.Credentials = new NetworkCredential(user, pass);
ftpRequest.KeepAlive = false;
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
ftpRequest.UseBinary = true;
byte[] messageContent = Encoding.ASCII.GetBytes(message);
ftpRequest.ContentLength = messageContent.Length;
int buffLength = 2048;
Stream ftpStream = ftpRequest.GetRequestStream();
int total_bytes = (int)messageContent.Length;
//this is the area I need the most help with
while (total_bytes < messageContent.Length)
{
ftpStream.Write(messageContent, total_bytes, buffLength);
total_bytes += buffLength;
var progress = total_bytes * 100.0 / messageContent.Length;
bkgd_Submit.ReportProgress((int)progress);
}
ftpStream.Close();
}
该程序跳过while
块,因为total_bytes
始终等于messageContent.Length
。 我对FTP上传的了解不足,无法知道缓冲的作用和应该做的不同。
非常感谢你的帮助!
您将total_bytes
设置为等于messageContent.Length
,然后检查是否小于该值。 当您将它们设置为相等时,怎么能少呢?
int total_bytes = (int)messageContent.Length;
//this is the area I need the most help with
while (total_bytes < messageContent.Length)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.