Click here to Skip to main content
11,504,689 members (69,257 online)
Click here to Skip to main content

Easy-to-use resumable .NET file downloader

, 25 Oct 2006 102.5K 2.3K 214
Rate this:
Please Sign up or sign in to vote.
A simple, easy-to-use, resumable .NET file downloader.

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

   // Listen for when the download completes.
   downloader.DownloadComplete +=
     new EventHandler(downloader_DownloadedComplete);

   // Listen as each new packet of data comes in.
   downloader.ProgressChanged +=
     new DownloadProgressHandler(downloader_ProgressChanged);

   // Download synchronously
   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)
{
   // DownloadEventArgs has lots of progress information
   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:
    • file:///c:/myfolder/myfile.txt, or
    • file://///networkMachine/sharedFolder/file.txt

      (Yes, that's five slashes.)

  • 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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Phil Crosby
Web Developer
United States United States
I'm a recent graduate of the University of Maryland. I have interned with the Visual Studio team at Microsoft and was an Extreme Blue Intern at IBM. I'm currently hacking on InstallPad.com, Mono applications for GNOME, and Ink for Word (Tablet PC annotation support for Microsft Word). Visit my blog or homepage.

Comments and Discussions

 
QuestionBug found?? Pin
i007-Feb-14 20:09
professional i007-Feb-14 20:09 
AnswerRe: Bug found?? Pin
i008-Feb-14 2:39
professional i008-Feb-14 2:39 
QuestionSpeed Pin
Tomáš Nariel Voznička27-Jan-14 12:46
memberTomáš Nariel Voznička27-Jan-14 12:46 
AnswerRe: Speed Pin
i008-Feb-14 2:34
professional i008-Feb-14 2:34 
QuestionAntivirus software slows it down sometimes Pin
Ed Sonneveld11-Mar-13 23:12
memberEd Sonneveld11-Mar-13 23:12 
GeneralMy vote of 5 Pin
Арчил Абуладзе28-Aug-12 3:10
memberАрчил Абуладзе28-Aug-12 3:10 
BugAsyncDownload doesn`t work Pin
Арчил Абуладзе28-Aug-12 3:07
memberАрчил Абуладзе28-Aug-12 3:07 
QuestionASP.net Pin
Eric Beck2-Nov-11 9:20
memberEric Beck2-Nov-11 9:20 
Questionthank for your api, its good Pin
subbunatarajan1-Aug-11 0:40
membersubbunatarajan1-Aug-11 0:40 
Generalgood job Pin
Eric__Liu21-Oct-10 17:14
memberEric__Liu21-Oct-10 17:14 
GeneralGreat class. Suggestion for next version Pin
Ascott0324-Nov-09 12:47
memberAscott0324-Nov-09 12:47 
GeneralThank you very much for this API Pin
radialronnie23-Jul-08 4:48
memberradialronnie23-Jul-08 4:48 
GeneralPhil. Thanks !!! a qeustion Pin
Member 151816719-Feb-08 11:31
memberMember 151816719-Feb-08 11:31 
QuestionGreat and easy solution Pin
mishenkovks21-Sep-07 2:04
membermishenkovks21-Sep-07 2:04 
GeneralMono Sharing Violation Pin
stclarence20-Jun-07 19:56
memberstclarence20-Jun-07 19:56 
QuestionHow can I use it in VB ? Pin
pongup10-May-07 10:41
memberpongup10-May-07 10:41 
AnswerRe: How can I use it in VB ? Pin
robertw01921-Jul-07 21:01
memberrobertw01921-Jul-07 21:01 
GeneralDownload from Tomcat Webdav Pin
roychoo16-Mar-07 2:22
memberroychoo16-Mar-07 2:22 
General407 authentication error Pin
Cohen Shwartz Oren7-Jan-07 7:19
memberCohen Shwartz Oren7-Jan-07 7:19 
GeneralRe: 407 authentication error Pin
veritas guy16-Jul-07 9:28
memberveritas guy16-Jul-07 9:28 
NewsUI HAS BEEN POSTED FINALLY Pin
Fred Johanns18-Aug-06 10:38
memberFred Johanns18-Aug-06 10:38 
GeneralFuture improvement Pin
TehPenguin9-Aug-06 15:20
memberTehPenguin9-Aug-06 15:20 
GeneralRe: Future improvement Pin
Phil Crosby25-Oct-06 9:22
memberPhil Crosby25-Oct-06 9:22 
GeneralReally nice Pin
ajai80851-Aug-06 1:30
memberajai80851-Aug-06 1:30 
GeneralNeat and simple! Pin
Super Lloyd29-Jul-06 5:00
memberSuper Lloyd29-Jul-06 5:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 25 Oct 2006
Article Copyright 2006 by Phil Crosby
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid