Click here to Skip to main content
Click here to Skip to main content

HTTP File Downloader Class for .NET

, 23 Sep 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

_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:

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)

Share

About the Author

Alex_1
Web Developer
Australia Australia
No Biography provided

Comments and Discussions

 
GeneralThis component has been superseded by HttpDownloaderControl PinmemberAlex_115-Jul-08 15:17 
Generalquestion Pinmemberjkersch24-Sep-07 1:08 
GeneralCool PinmemberRunnerDude6-Jul-07 9:30 
GeneralLooks good PinmemberTmefjhf28-Jun-07 17:30 
GeneralRe: Looks good Pinmemberrohim.yasin4-Jul-07 3:04 

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
Web01 | 2.8.141223.1 | Last Updated 23 Sep 2007
Article Copyright 2007 by Alex_1
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid