簡體   English   中英

FTP使用C#和.NET4使用后台工作程序和進度條從內存中上傳字符串

[英]FTP Upload string from memory with background worker and progress bar using C# and .NET4

我的問題與這一問題相似,但是我無法在我的應用程序中使用它們中的任何一個。 也許我缺少什么?

這是我要完成的工作:

  • 通過FTP 從內存中上傳字符串
  • 顯示和更新后台工作人員進度條

這是我現在擁有的代碼:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM