【发布时间】:2011-06-19 22:18:43
【问题描述】:
我在使用 C# WebClient DownloadFileAsync 时遇到了一些问题,希望您能帮助我。
这是我的场景:
我正在使用 WebClient DownloadFileAsync 同时下载许多文件(http://example.com/abc/1.jpg、http://example.com/abc/efg/2.jpg 等)。
我当前的示例代码是:
while (theres still files in http://example.com/abc/) {
// filename will be something like /abc/1.jpg, /abc/efg/2.jpg
if (!file.exists(directory.getcurrentdirectory()+filename.Replace('/', '\\'))) {
WebClient webClient = new WebClient();
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(downloadProgressChanged);
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(downloadCompleted);
webClient.DownloadFileAsync(new Uri("http://example.com"+filename), Directory.GetCurrentDirectory()+filename.Replace('/', '\\'));
}
如何使正在下载的所有文件都显示在一个进度条中?
例如。 1.jpg为50kb,2.jpg为50kb,1.jpg下载完成后进度条会显示50%,2.jpg会在进度条中从51%到100%。
另外,如果文件名是/abc/1.jpg,如果我的当前目录没有名为abc的文件夹,下载将无法进行。如何让它根据文件名自动创建文件夹? (例如/random123/3.jpg、/anotherrandom/4.jpg等)
【问题讨论】:
-
你在问两个不相关的事情,你应该问两个问题。
-
开始下载之前你知道所有图片的大小吗?还是应该让进度条与图片大小无关?
-
在开始下载之前,我不知道所有图像的总大小。它应该是动态的。
-
那么,如果有两张图片应该显示什么:我已经下载完一张50kB的图片,但我还不知道另一张的大小?跨度>
-
有什么方法可以计算我到目前为止下载的总字节数吗?从我上面的代码。
标签: c# webclient progress downloadfileasync