I have this C# code but the final esi.zip results in 0 length or basically empty. The URL does exist and confirms to download the file manually. I am baffled 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)
Thanks
UPDATED: I updated the code where no spaces exist at all but it still downloads 0 bytes.
The simply way how to download file is to use WebClient class and its method DownloadFile. This method has two parameters, first is the url of the file you want to download and the second parameter is path to local disk to which you want to save the file.
The DownloadData method downloads the resource with the URI specified by the address parameter. This method blocks while downloading the resource. To download a resource and continue executing while waiting for the server's response, use one of the DownloadDataAsync methods.
Here's the working code. There were 2 things you were not doing, that was causing the 0
byte file to be downloaded.
IsBusy
. That needs to be called in order for the code to wait for the current thread to complete, since the an async action will be on a new thread.Create a blank console app and put the following code in it and try it out.
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);
}
}
}
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!!!");
}
}
}
And when it's complete, you should see the zip file in the location specified in the targetdownloadedFile
variable, which in this example is at C:\Temp\TestZip.zip
on your local machine.
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) { }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With