Why is the DownloadFileAsync method in my WebClient downloading an empty file?

I have this C # code, but the final esi.zip results in 0 length or mostly empty. The url exists and confirms manual download of the file. I am confusing, buy this.

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)

      

thank

UPDATED: I updated the code where there are no spaces, but it still downloads 0 bytes.

+3


source to share


2 answers


Here's the working code. There were 2 things you didn't do that was causing the byte file to load 0

.

  • You haven't called IsBusy

    . This needs to be called in order for the code to wait for the current thread to complete, since the async action will be on the new thread.
  • The site in question returns badgateway unless you spoof the request as if it were coming from a regular web browser.

Create a blank console app and put the following code in it and try it out.

Paste this code into the Program.cs file of an empty / new console application.

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);
        }
    }
}

      



Add a new C # class file called DownloadManager and leave this code in there.

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!!!");
        }
    }
}

      

Now create and run a console application. You should see progress in the console output window this way.

And when it finishes, you should see the zip file at the location specified in the variable targetdownloadedFile

, which in this example is C:\Temp\TestZip.zip

on your local machine.

+8


source


 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) { }

      



0


source







All Articles