简体   繁体   English

使用WebClient下载显示进度

[英]Show Progress Using WebClient Download

I am testing some features of WebClient class and I decided to see how DownloadProgressChanged works so I made up such code: 我正在测试WebClient类的某些功能,因此决定查看DownloadProgressChanged工作原理,因此编写了以下代码:

static void Main(string[] args)
{
  WebClient client = new WebClient();
  client.Proxy = null;
  client.BaseAddress = "ftp://ftp.xxxxxxx.com";
  CredentialCache cache = new CredentialCache();
  NetworkCredential credential = new NetworkCredential("userxxx", "passxxxx");
  client.Credentials = credential;
  client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
  Uri remoteFile;
  Uri.TryCreate("/public_html/folderxxxx/Pictures/Product/Resized/1.jpg", System.UriKind.Relative, out remoteFile);

  client.DownloadFileAsync(remoteFile, "1.jpg");
  System.Diagnostics.Process.Start("1.jpg");
  Console.ReadLine();
}

static void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
  Console.WriteLine(e.ProgressPercentage.ToString());
}

When I run this application, this is what I see as progress: 当我运行此应用程序时,这是我看到的进度:

控制台应用程序显示进度

Looks nothing fancy. 看起来没什么花哨的。 Ben Albahari suggest using new Thread instead of using Async method and this event handler is useful when you actually use Async method. 本·阿尔巴哈里(Ben Albahari)建议使用新线程而不是使用异步方法,并且当您实际使用异步方法时,此事件处理程序很有用。

So how can I show Progress truly? 那么我如何才能真正展现进步?

EDIT: 编辑:

According to MSDN this should be done: 根据MSDN,应执行以下操作:

A passive FTP file transfer will always show a progress percentage of zero, since the server did not send the file size. 由于服务器未发送文件大小,因此被动FTP文件传输将始终显示进度百分比为零。 To show progress, you can change the FTP connection to active by overriding the GetWebRequest virtual method: 为了显示进度,您可以通过覆盖GetWebRequest虚拟方法将FTP连接更改为活动状态:

Sample Code: 样例代码:

   internal class MyWebClient:WebClient{
        protected override WebRequest GetWebRequest(Uri address) {
            FtpWebRequest req = (FtpWebRequest)base.GetWebRequest(address);
            req.UsePassive = false;
            return req;
        }
    }

Which is what I did but the same problem happens: 这是我所做的,但是发生相同的问题:

在此处输入图片说明

From MSDN: 从MSDN:

A passive FTP file transfer will always show a progress percentage of zero, since the server did not send the file size. 由于服务器未发送文件大小,因此被动FTP文件传输将始终显示进度百分比为零。 To show progress, you can change the FTP connection to active by overriding the GetWebRequest virtual method. 为了显示进度,您可以通过覆盖GetWebRequest虚拟方法将FTP连接更改为活动状态。

Source 资源

There seem to be a lot of people asking this question. 似乎有很多人问这个问题。

So far as I can tell your server will only report back progress if it is set you allow active connections. 据我所知,如果设置为允许活动连接,则服务器将仅报告进度。 It looks like PASV (passive FTP) can be enabled or disabled on the server. 看起来可以在服务器上启用或禁用PASV(被动FTP)。 And the mode used should be determined by what the server is set to use and what is between server and client. 所使用的模式应由服务器要使用的设置以及服务器和客户端之间的设置确定。

EDIT: I enabled System.Net.Tracing on my project and tried both passive and active modes and not only do both work as expected... TotalBytes is still -1 so my thinking is the note on MSDN 编辑:我在我的项目上启用了System.Net.Tracing并尝试了被动和主动模式,不仅两者都按预期方式工作... TotalBytes仍然为-1,所以我的想法是在MSDN上进行注释

http://msdn.microsoft.com/en-US/library/system.net.webclient.downloadprogresschanged(v=vs.80).aspx http://msdn.microsoft.com/en-US/library/system.net.webclient.downloadprogresschanged(v=vs.80).aspx

is mistaken or we are missing something. 弄错了,或者我们错过了一些东西。

http://compnetworking.about.com/cs/protocolsftp/g/bldef_pasv.htm http://compnetworking.about.com/cs/protocolsftp/g/bldef_pasv.htm

http://slacksite.com/other/ftp.html http://slacksite.com/other/ftp.html

http://fetchsoftworks.com/fetch/help/Contents/Concepts/ActiveAndPassive.html http://fetchsoftworks.com/fetch/help/Contents/Concepts/ActiveAndPassive.html

So overriding the WebRequest method will make no difference if the server does not use active connections. 因此,如果服务器不使用活动连接,则覆盖WebRequest方法不会有任何区别。

However... as you can see from your own command line readout it does report back the BytesReceived correctly. 但是...从您自己的命令行读数中可以看到,它确实报告了正确接收的BytesReceived。

Find out the file size and you're away... 找出文件大小,您就可以离开...

I expect there is a more efficient method but... I used an FtpWebRequest to get the file size before downloading and then run DownloadFileAsync and pass the file size to the DownloadProgressCallback using: 我希望有一种更有效的方法,但是...我在下载之前使用FtpWebRequest获取文件大小,然后运行DownloadFileAsync,并使用以下命令将文件大小传递给DownloadProgressCallback:

Client.DownloadProgressChanges += (sender, e) => DownloadProgressCallback( fileSize, e)

Hope this helps :) 希望这可以帮助 :)

Did you read the documentation ?? 您阅读过文档吗?

A passive FTP file transfer will always show a progress percentage of zero, since the server did not send the file size. 由于服务器未发送文件大小,因此被动FTP文件传输将始终显示进度百分比为零。 To show progress, you can change the FTP connection to active by overriding the GetWebRequest virtual method: 为了显示进度,您可以通过覆盖GetWebRequest虚拟方法将FTP连接更改为活动状态:

internal class MyWebClient:WebClient{
        protected override WebRequest GetWebRequest(Uri address) {
            FtpWebRequest req = (FtpWebRequest)base.GetWebRequest(address);
            req.UsePassive = false;
            return req;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM