[英]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.