繁体   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