WebClient - 第二次调用时 DownloadFileAsync 不起作用 [英] WebClient - DownloadFileAsync not working when called second time

查看:27
本文介绍了WebClient - 第二次调用时 DownloadFileAsync 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了下载单个文件的简单方法.当我第一次调用该方法时一切正常,但第二次调用时文件未下载.
下面是我的代码:

I've build simple method that downloads single file. When I call that method first time everything works fine, but when called second time file isn't downloaded.
Below is my code:

public void DownloadFile(string fileUrl, string path)
{
    using (var webClient = new WebClient())
    {
        webClient.DownloadFileCompleted += (sender, e) =>
        {
            if (e.Error == null & !e.Cancelled)
            {
                Debug.WriteLine(@"Download completed!");
            }
        };

        var url = new Uri(fileUrl);

        try
        {
            webClient.OpenRead(url);
            string headerContentDisposition = webClient.ResponseHeaders["content-disposition"];
            string filename = new ContentDisposition(headerContentDisposition).FileName;

            Debug.WriteLine(filename);

            path = Path.Combine(path, filename);
            webClient.DownloadFileAsync(url, path);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

我已经确定了破坏我下载的部分,它是负责获取文件名的部分:

I've identified part that is breaking my download, it is part that is responsible for getting file name:

webClient.OpenRead(url);
string headerContentDisposition = webClient.ResponseHeaders["content-disposition"];
string filename = new ContentDisposition(headerContentDisposition).FileName;

如果我将那部分替换为 string filename = "1.tmp"; 我可以多次调用我的方法而不会出错.

If I replace that part with string filename = "1.tmp"; I'm able to call my method multiple times without errors.

我通过单击带有此单击事件的按钮来调用该方法:

I'm calling that method by clicking button with this click event:

private void button1_Click(object sender, EventArgs e)
{
    const string url = @"http://www.jtricks.com/download-text";
    const string target = @"D:\TEMP\";
    DownloadFile(url, target);
}

点击两次没有获取文件名的代码的按钮后,我在控制台中得到了这个输出:

After two click on button without code that is getting file name I get this output in console:

1.tmp
Download completed!
1.tmp
Download completed!

下面是显示这个工作正常的 gif:

Below is gif showing this working fine:

当我添加回获取文件名的部分时,这是我的输出:

When I add back part that is getting file name this is my output:

content.txt
Download completed!
content.txt

下面的 gif 显示了这种行为:

Below gif showing that behavior:

我第二次点击开始我得到文件名,但下载没有开始,接下来点击阻止开始按钮.

Second time I click Start I'm getting file name, but download don't start, next click blocks start button.

我该如何解决这个问题?理想情况下,我想根据需要多次调用 DownloadFile.

How can I fix this? Ideally I'd like to call DownloadFile as many time I need.

推荐答案

看来 WebClient 正在使用缓存.我建议你必须告诉 WebClient 不要使用缓存:

It seems that WebClientis using a Cache. I suggest that you have to tell WebClient not to use caching:

webClient.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);

这篇关于WebClient - 第二次调用时 DownloadFileAsync 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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