c# - 为什么在我的 WebClient DownloadFileAsync 方法中下载空文件?

标签 c# url download

我有此 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 字节。

最佳答案

这是工作代码。有 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 在您的本地计算机上。

关于c# - 为什么在我的 WebClient DownloadFileAsync 方法中下载空文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26534980/

相关文章:

python - 如何使用 Python Mechanize 从基于 Web 的文件服务器下载文件

c# - Visual Studio 2012没有调试选项

javascript - 哪里是存储非代码文件(例如 mp3)以便使用 codepen 进行 javascript 测试的好地方?

javascript - 将变量从 HREF 传递到 Google HTMLSERVICE 应用程序

下载网页的工作本地副本

c# - 使用 C# 在 ASP.Net 中下载文件

c# - 在 C# 和 mysql 中制作相同的 sha512

c# - Windows Mobile 上的 vector 图形

c# - 如何将函数扩展到 Random 类

url - 是否可以使用 ColdFusion 重写 url?