Introduction
This is a simple-to-use file downloader class for use in .NET projects. The implementation is small, and the API is clean. It supports resuming, and has descriptive error exceptions.
Background
This is based off of John Batte's initial CodeProject implementation, which worked well. I used his code for InstallPad, and felt it necessary to make a few improvements and share them.
- Greatly simplified API
- Supports ftp:// and file:// URLs
- Informative progress information
- Obtains file name from the server, which allows downloading from URLs with query strings, e.g., http://server.com/?file
- Removal of WaitHandles
- Distributed as a .cs file instead of an unfortunate EXE installer
- Download from a list of URLs, in case one of the URLs fails
- Proxy support
- Descriptive exceptions - you'll get something like "Error saving file (file)" instead of "Invalid characters in path", with the original exception accessible as an inner exception
- Compiles without warnings in Visual Studio 2005
Using the code
Using the downloader is very simple. You can call it synchronously or asynchronously. Calling it synchronously will download the entire file before progressing to the next line of the method. Calling it asynchronously will let the execution of your method proceed while the downloader works in the background. You can listen to the progress of the download by subscribing to its events. Here is a simple sample which downloads FireFox:
static void Main(string[] args)
{
FileDownloader downloader = new FileDownloader();
downloader.DownloadComplete +=
new EventHandler(downloader_DownloadedComplete);
downloader.ProgressChanged +=
new DownloadProgressHandler(downloader_ProgressChanged);
downloader.Download("http://download.mozilla.org/" +
"?product=firefox-1.5.0.4&os=win&lang=en-US");
}
static void downloader_ProgressChanged(object sender, DownloadEventArgs e)
{
Console.WriteLine("Progress " + e.PercentDone);
}
static void downloader_DownloadedComplete(object sender, EventArgs e)
{
Console.WriteLine("Download complete.");
}
You can begin a download by using one of the following methods:
Download(string url)
Download(string url, string destFolder)
Download(List<String> urlList)
Download(List<String> urlList, string destFolder)
There are asynchronous versions of each:
AsyncDownload(string url)
AsyncDownload(string url, string destFolder)
AsyncDownload(List<String> urlList)
AsyncDownload(List<String> urlList, string destFolder)
And you can cancel a download anytime with the Cancel()
method.
This code also supports using a proxy. You can build an IWebProxy
object and assign it to the downloader through the downloader's Proxy
property.
Future work
Anyone can contribute:
- Bugs: the code may not be bug free, so any bug reports will get fixed.
- Support credentials for accessing secure download sites.
History
10/25/2006 - Update
- Support for downloading from a list of URLs in case one of them is down (Zac Ruiz)
- Added support for proxies
7/31/2006 - Update
- Now supports ftp:// and file:// URLs (only HTTP is resumable, and supporting these adds a hard requirement to .NET 2.0). File URLs must be of the following form:
- It's now possible to download a 0 byte file (an empty placeholder file gets created on disk)
- More accurate exceptions in some cases
6/10/2006 - Initial version