我们如何显示使用FtpWebRequest进行上传的进度条 [英] How can we show progress bar for upload with FtpWebRequest

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

问题描述

我使用 FtpWebRequest 将文件上传到ftp。我需要显示已完成工作的状态。



到目前为止,我的代码是:

  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;

//从Uri提供的
创建FtpWebRequest对象reqFTP =(FtpWebRequest)FtpWebRequest.Create(new Uri(uri));

//提供WebPermission Credintials
reqFTP.Credentials = new NetworkCredential(Username,Password);

//默认情况下,KeepAlive为true,控制连接未关闭
//执行命令后。
reqFTP.KeepAlive = false;
//reqFTP.UsePassive = true;
//指定要执行的命令。
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

//指定数据传输类型。
reqFTP.UseBinary = true;

//通知服务器上传文件的大小
reqFTP.ContentLength = fileInf.Length;

//缓冲区大小设置为2kb
int buffLength = 2048;
byte [] buff = new byte [buffLength];
int contentLen;

//打开一个文件流(System.IO.FileStream)来读取要上传的文件
FileStream fs = fileInf.OpenRead();

//将要上载的文件写入的流
Stream strm = reqFTP.GetRequestStream();

//一次从文件流2kb读取
contentLen = fs.Read(buff,0,buffLength);

//直到流内容结束
while(contentLen!= 0)
{
//将文件流中的内容写入FTP上传流
strm.Write(buff,0,contentLen);
contentLen = fs.Read(buff,0,buffLength);
}

//关闭文件流和请求流
strm.Close();
fs.Close();


解决方案

最简单的方法是使用 BackgroundWorker 并将代码放入 DoWork 事件处理程序中。并报告 BackgroundWorker.ReportProgress



的基本思路:

  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; ((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);



$ b private void backgroundWorker1_ProgressChanged(object sender,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天全站免登陆