使用C#和.NET4,使用后台工作程序和进度条从内存中FTP上载字符串

使用C#和.NET4,使用后台工作程序和进度条从内存中FTP上载字符串,c#,.net-4.0,ftp,backgroundworker,C#,.net 4.0,Ftp,Backgroundworker,我的问题类似于和,但我无法让它们中的任何一个在我的应用程序中工作。也许我遗漏了什么 以下是我想要实现的目标: 通过FTP从内存上传字符串 使用后台工作程序显示和更新进度条 这是我现在的代码: private void bkgd_Submit_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { string host = @"ftp://ftp.somesite.net/submissions/";

我的问题类似于和,但我无法让它们中的任何一个在我的应用程序中工作。也许我遗漏了什么

以下是我想要实现的目标:

  • 通过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();
}
private void bkgd\u Submit\u DoWork(对象发送方,System.ComponentModel.DoWorkEventArgs e)
{
字符串主机=@“ftp://ftp.somesite.net/submissions/";
字符串user=“username”;
字符串pass=“password”;
字符串[]行={“第一行”、“第二行”、“第三行”};
字符串ftpFile=host+“filename.txt”;
//需要上传的字符串
string message=“您好,这是第一批订单。”;
FtpWebRequest ftpRequest=(FtpWebRequest)FtpWebRequest.Create(ftpFile);
ftpRequest.Credentials=新网络凭据(用户,通过);
ftpRequest.KeepAlive=false;
ftpRequest.Method=WebRequestMethods.Ftp.UploadFile;
ftpRequest.UseBinary=true;
byte[]messageContent=Encoding.ASCII.GetBytes(消息);
ftpRequest.ContentLength=messageContent.Length;
int buffLength=2048;
流ftpStream=ftpRequest.GetRequestStream();
int total_bytes=(int)messageContent.Length;
//这是我最需要帮助的领域
while(总字节
程序跳过
while
块,因为
总字节数始终等于
messageContent.Length
。我对FTP上传了解不够,不知道缓冲区的作用以及我应该做些什么


非常感谢你的帮助

您正在将
总字节数
设置为等于
messageContent.Length
,然后检查是否小于该值。当你把它们设置成相等的时候,怎么能减少呢

int total_bytes=(int)messageContent.Length;
//这是我最需要帮助的领域
while(总字节
我肯定我看起来像一个彻头彻尾的傻瓜:),但这个()答案正是我这样做的原因。我对FTP上传没有很好的理解,因此如果您能解释一下
while
循环应该是什么样子,我将非常感激。在您链接的示例中,
total_bytes
被设置为
messageContent.Length
。但是,示例中的条件是
while(total_bytes>0)
,而不是
while(total_bytes
。基本上,他在写
x
字节数,然后从
total_bytes
中减去
x
,直到
total_bytes==0
,这意味着没有更多的字节要发送。我不知道ftpStream是如何工作的,但看起来您可能希望首先将
total_bytes
设置为零,然后在每次写入后添加字节数,直到
total_bytes
等于整个文件的长度。
int total_bytes = (int)messageContent.Length;

//this is the area I need the most help with
while (total_bytes < messageContent.Length)