Click here to Skip to main content
Licence CPOL
First Posted 28 Jun 2007
Views 33,190
Downloads 864
Bookmarked 89 times

HTTP File Downloader Class for .NET

By | 23 Sep 2007 | Article
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)

About the Author

Alex_1

Web Developer

Australia Australia

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThis component has been superseded by HttpDownloaderControl PinmemberAlex_114:17 15 Jul '08  
Generalquestion Pinmemberjkersch0:08 24 Sep '07  
GeneralCool PinmemberRunnerDude8:30 6 Jul '07  
GeneralLooks good PinmemberTmefjhf16:30 28 Jun '07  
GeneralRe: Looks good Pinmemberrohim.yasin2:04 4 Jul '07  

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.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 23 Sep 2007
Article Copyright 2007 by Alex_1
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid