【问题标题】:Why in my WebClient DownloadFileAsync method downloading an Empty File?为什么在我的 WebClient DownloadFileAsync 方法中下载一个空文件?
【发布时间】:2014-10-23 18:36:20
【问题描述】:

我有这个 C# 代码,但最终的 esi.zip 导致长度为 0 或基本上为空。该 URL 确实存在并确认手动下载文件。我很困惑买这个。

string zipPath = @"C:\download\esi.zip";

Client.DownloadFileAsync(new Uri("http://ec.europa.eu/economy_finance/db_indicators  
 /surveys/documents/series/nace2_ecfin_1409/all_surveys_total_sa_nace2.zip"), zipPath)

谢谢

更新:我更新了根本不存在空格的代码,但它仍然下载了 0 个字节。

【问题讨论】:

  • 图片 URL 中的那个巨大空间应该在那里吗?还是在您的代码中?删除该空间是否可以解决问题?
  • 你等待DownloadFileCompleted事件了吗?
  • 代码中没有空格,似乎添加这个stackoverflow可能很奇怪。 Shiva,你能把整个 C# 文件发送过来,这样我就可以看到我的结局是错误的吗?谢谢
  • 在此处查看代码 => dotnetfiddle.net/vjZAe2 由于FileIOSecurityException,小提琴将无法运行。但是将此代码放在您的本地控制台应用程序中。这就是我所做的,它运行并保存文件,但它是0 字节,就像你说的那样。很奇怪。
  • 我修好了。可以下载代码。将发布它。您必须在WebClient 上致电IsBusy 等待下载。此外,如果您未指定浏览器用户代理,则该 URL/站点将返回 502 Bad Gateway

标签: c# url download


【解决方案1】:

这是工作代码。有 2 件事您没有做,导致 0 字节文件被下载。

  1. 你没有打电话给IsBusy。需要调用它以使代码等待当前线程完成,因为异步操作将在新线程上。
  2. 相关站点返回了一个错误网关,除非您将请求伪装成来自常规网络浏览器的请求。

创建一个空白控制台应用程序并将以下代码放入其中并尝试一下。

将此代码粘贴到空白/新控制台应用程序的 Program.cs 文件中。

namespace TestDownload
{
    class Program
    {
        static void Main(string[] args)
        {
            string sourceUrl = "http://ec.europa.eu/economy_finance/db_indicators/surveys/documents/series/nace2_ecfin_1409/all_surveys_total_sa_nace2.zip";
            string targetdownloadedFile = @"C:\Temp\TestZip.zip";
            DownloadManager downloadManager = new DownloadManager();
            downloadManager.DownloadFile(sourceUrl, targetdownloadedFile);
        }
    }
}

添加一个名为 DownloadManager 的新 C# 类文件并将此代码放入其中。

using System;
using System.ComponentModel;
using System.Net;

namespace TestDownload
{
    public class DownloadManager
    {
        public void DownloadFile(string sourceUrl, string targetFolder)
        {
            WebClient downloader = new WebClient();
                // fake as if you are a browser making the request.
            downloader.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0)");
            downloader.DownloadFileCompleted += new AsyncCompletedEventHandler(Downloader_DownloadFileCompleted);
            downloader.DownloadProgressChanged +=
                new DownloadProgressChangedEventHandler(Downloader_DownloadProgressChanged);
            downloader.DownloadFileAsync(new Uri(sourceUrl), targetFolder);
                // wait for the current thread to complete, since the an async action will be on a new thread.
            while (downloader.IsBusy) { }
        }

        private void Downloader_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            // print progress of download.
            Console.WriteLine(e.BytesReceived + " " + e.ProgressPercentage);
        }

        private void Downloader_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
                // display completion status.
            if (e.Error != null)
                Console.WriteLine(e.Error.Message);
            else
                Console.WriteLine("Download Completed!!!");
        }
    }
}

现在构建并运行控制台应用程序。您应该会在控制台输出窗口中看到进度,如下所示。

完成后,您应该会在 targetdownloadedFile 变量中指定的位置看到 zip 文件,在本示例中,该位置位于本地计算机上的 C:\Temp\TestZip.zip

【讨论】:

  • 我更喜欢 Mohsin Kahn 的回答,但对于用户代理 +1。
  • while (downloader.IsBusy) { } - 这解决了我的问题。
【解决方案2】:
 objFeedBO = new FeedBO();
 string strfilename = System.IO.Path.GetFileName(url);
 FileStream outputStream = new FileStream(DownloadPath + "\\" + strfilename, FileMode.Create);

 string targetdownloadedFile = @"D:\TestZip.php";

 WebClient myWebClient = new WebClient();
 myWebClient.DownloadFileAsync(new Uri(url), targetdownloadedFile);
 while (myWebClient.IsBusy) { }

【讨论】:

  • 我认为这个回应值得更多关注!简单但有效。
【解决方案3】:

虽然这是一个旧线程,但到目前为止给出的答案可能不足以解决“空文件”问题。

如果您从 https 安全站点下载,“DownloadComplete”回调可能表示下载无错误,但下载的文件可能为空。 就我而言,here 给出的建议有所帮助。添加以下行后,下载就可以了:

    System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
    webClient.DownloadFileAsync(new System.Uri(link), savePath);

【讨论】:

    【解决方案4】:

    添加这个对我来说效果很好!谢谢!

    System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls | System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls12;
    

    【讨论】:

      猜你喜欢
      • 2012-11-11
      • 2018-01-06
      • 1970-01-01
      • 2013-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多