Click here to Skip to main content
15,891,473 members
Articles / Programming Languages / C#
Article

HTTP File Downloader Class for .NET

Rate me:
Please Sign up or sign in to vote.
4.85/5 (19 votes)
23 Sep 2007CPOL 56.2K   2.2K   92   5
HTTP File Downloader Class for .NET (HTTPFileDownloader)
Screenshot - FileDownloader.gif

Introduction

This short article presents the component HttpFileDownloader for .NET. It was designed a few years ago, that is why it is written for .NET 1.1. However it can be used for newer Frameworks and/or recompiled. It shows the estimated time left and the progress. The component can be used with Windows forms application and with console/service apps.
If used in a windowless environment, null should be passed to the constructor instead of this. The code is very simple to use and understand.

Using the Code

Create the instance of the component and the event handler.

C#
_FileDownloader = new DotNetFileDownloader(this); 
// "this" required only for Windows Forms Component/App 
_FileDownloader.URLDownload = textBox_url.Text; // set the URL to download
// setting local path
_FileDownloader.LocalFilePath = textBox_local_folder.Text + "\\" + FileName ;

// setting the downloading status event handler
_FileDownloader.UpdateStatusEvent +=new UpdateDelegate(
    _FileDownloader_UpdateStatusEvent); 

The status event handler is pretty straightforward:

C#
string Message, 
DStatus Status, 
long FullSize, 
long CurrentBytes, 
TimeSpan EstimatedTimeLeft)
{
    label_status.Text = Message + "\n" + Status.ToString();
    if (FullSize != 0 && FullSize != -1)
    {
        this.progressBar1.Value = (int)(((double)CurrentBytes / (double)FullSize) * 100);

        // display total Kbytes and current Kbytes
        label_curr_bytes.Text = CurrentBytes.ToString() + " Kb of " +
            FullSize.ToString() + " Kb"; 
    }

    if ((Status == DStatus.complete) || 
        (Status == DStatus.failed) || 
        (Status == DStatus.aborted))
        {
            button_start.Enabled = true;
            button_stop.Enabled = false;
        }
    // displaying estimated time left
    label_estimated_time.Text = "Estimated time left";
    string stmp = EstimatedTimeLeft.ToString();
    stmp = stmp.Substring(0, stmp.IndexOf('.'));
    label_estimated_time.Text = "Estimated time left : " + stmp; 
} 

If required, it can download through the proxy (with user name and password). The downloading can be aborted at any time because it runs in the worker thread.

Points of Interest

If a newer version of this component is released, it can be found here along with other useful classes and components.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThis component has been superseded by HttpDownloaderControl Pin
Alex_115-Jul-08 14:17
Alex_115-Jul-08 14:17 
Generalquestion Pin
jkersch24-Sep-07 0:08
jkersch24-Sep-07 0:08 
GeneralCool Pin
HankiDesign6-Jul-07 8:30
HankiDesign6-Jul-07 8:30 
I like that, cool thanks.. Smile | :)
GeneralLooks good Pin
qwecoder28-Jun-07 16:30
qwecoder28-Jun-07 16:30 
GeneralRe: Looks good Pin
rohim.yasin4-Jul-07 2:04
professionalrohim.yasin4-Jul-07 2:04 

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

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