我们怎样才能显示具有的FtpWebRequest进度条 [英] How can we show progress bar with FtpWebRequest

查看:171
本文介绍了我们怎样才能显示具有的FtpWebRequest进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我上传文件使用的FtpWebRequest 到FTP。我需要显示的状态中有多少是完成的。

到目前为止,我的code是:

 公共无效上传(字符串的文件名,字符串URL)
{
    FileInfo的fileInf =新的FileInfo(文件名);
    字符串URI =FTP://+网址+/+ fileInf.Name;
    的FtpWebRequest reqFTP;
    //字符串URI =FTP://+主机+/的public_html /测试/ blogtest /+ fileInf.Name;    //创建的FtpWebRequest对象从乌里提供
    reqFTP =(的FtpWebRequest)FtpWebRequest.Create(新的URI(URI));    //提供的WebPermission的Credintials
    reqFTP.Credentials =新的NetworkCredential(用户名,密码);    //默认的KeepAlive是真实的,其中控制连接未关闭
    //后执行的命令。
    reqFTP.KeepAlive = FALSE;
    //reqFTP.UsePassive = TRUE;
    //指定要执行的命令。
    reqFTP.Method = WebRequestMethods.Ftp.UploadFile;    //指定数据传输类型。
    reqFTP.UseBinary = TRUE;    //通知服务器对上传文件的大​​小
    reqFTP.ContentLength = fileInf.Length;    //缓冲区大小被设定为2KB
    INT buffLength = 2048;
    字节[] = BUFF新的字节[buffLength]
    INT contentLen;    //打开文件流(System.IO.FileStream)来读取文件上传
    的FileStream FS = fileInf.OpenRead();    //要载流该文件被写入
    流STRM = reqFTP.GetRequestStream();    //从文件流2KB一次读取
    contentLen = fs.Read(BUFF,0,buffLength);    //直到流内容结束
    而(contentLen!= 0)
    {
        从文件流//内容写入FTP上载流
        strm.Write(BUFF,0,contentLen);
        contentLen = fs.Read(BUFF,0,buffLength);
    }    //关闭文件流和请求流
    strm.Close();
    fs.Close();
}


解决方案

最简单的就是使用的BackgroundWorker ,把你的code到 DoWork的事件处理程序。并与 BackgroundWorker.ReportProgress 报告进度。

其基本思路:

 私人无效backgroundWorker1_DoWork(对象发件人,DoWorkEventArgs E)
{
    变种的FtpWebRequest =(的FtpWebRequest)WebRequest.Create(ftp://example.com);
    ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
    使用(VAR的InputStream = File.OpenRead(文件名))
    使用(VAR的OutputStream = ftpWebRequest.GetRequestStream())
    {
        VAR缓冲=新的字节[1024 * 1024];
        INT totalReadBytesCount = 0;
        INT readBytesCount;
        而((readBytesCount = inputStream.Read(缓冲液,0,buffer.Length))大于0)
        {
            outputStream.Write(缓冲液,0,readBytesCount);
            totalReadBytesCount + = readBytesCount;
            VAR进度= totalReadBytesCount * 100.0 / inputStream.Length;
            backgroundWorker1.ReportProgress((int)的进度);
        }
    }
}私人无效backgroundWorker1_ProgressChanged(对象发件人,ProgressChangedEventArgs E)
{
    progressBar.Value = e.ProgressPercentage;
}

确认 WorkerReportsProgress 已启用

  backgroundWorker2.WorkerReportsProgress = TRUE;

使用的BackgroundWorker 你也可以轻松实现上传取消。

I am uploading files to ftp using FtpWebRequest. I need to show the status that how much is done.

So far my code is:

public void Upload(string filename, string url)
{
    FileInfo fileInf = new FileInfo(filename);
    string uri = "ftp://" + url + "/" + fileInf.Name;
    FtpWebRequest reqFTP;
    //string uri = "ftp://" + Host + "/public_html/testing/blogtest/" + fileInf.Name;

    // Create FtpWebRequest object from the Uri provided
    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));

    // Provide the WebPermission Credintials
    reqFTP.Credentials = new NetworkCredential(Username, Password);

    // By default KeepAlive is true, where the control connection is not closed
    // after a command is executed.
    reqFTP.KeepAlive = false;
    //reqFTP.UsePassive = true;
    // Specify the command to be executed.
    reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

    // Specify the data transfer type.
    reqFTP.UseBinary = true;

    // Notify the server about the size of the uploaded file
    reqFTP.ContentLength = fileInf.Length;

    // The buffer size is set to 2kb
    int buffLength = 2048;
    byte[] buff = new byte[buffLength];
    int contentLen;

    // Opens a file stream (System.IO.FileStream) to read the file to be uploaded
    FileStream fs = fileInf.OpenRead();

    // Stream to which the file to be upload is written
    Stream strm = reqFTP.GetRequestStream();

    // Read from the file stream 2kb at a time
    contentLen = fs.Read(buff, 0, buffLength);

    // Till Stream content ends
    while (contentLen != 0)
    {
        // Write Content from the file stream to the FTP Upload Stream
        strm.Write(buff, 0, contentLen);
        contentLen = fs.Read(buff, 0, buffLength);
    }

    // Close the file stream and the Request Stream
    strm.Close();
    fs.Close();
}

解决方案

The easiest is to use BackgroundWorker and put your code into DoWork event handler. And report progress with BackgroundWorker.ReportProgress.

The basic idea:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    var ftpWebRequest = (FtpWebRequest)WebRequest.Create("ftp://example.com");
    ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
    using (var inputStream = File.OpenRead(fileName))
    using (var outputStream = ftpWebRequest.GetRequestStream())
    {
        var buffer = new byte[1024 * 1024];
        int totalReadBytesCount = 0;
        int readBytesCount;
        while ((readBytesCount = inputStream.Read(buffer, 0, buffer.Length)) > 0)
        {
            outputStream.Write(buffer, 0, readBytesCount);
            totalReadBytesCount += readBytesCount;
            var progress = totalReadBytesCount * 100.0 / inputStream.Length;
            backgroundWorker1.ReportProgress((int)progress);
        }
    }
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    progressBar.Value = e.ProgressPercentage;
}

Make sure WorkerReportsProgress is enabled

backgroundWorker2.WorkerReportsProgress = true;

With BackgroundWorker you can also easily implement upload cancellation.

这篇关于我们怎样才能显示具有的FtpWebRequest进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆