Click here to Skip to main content
6,306,412 members and growing! (18,388 online)
Email Password   helpLost your password?
General Programming » Internet / Network » HTTP     Intermediate License: The Code Project Open License (CPOL)

HTTP File Downloader Class for .NET

By Alex_1

HTTP File Downloader Class for .NET (HTTPFileDownloader)
C#, Windows, .NET, Visual Studio, Dev
Posted:28 Jun 2007
Updated:23 Sep 2007
Views:22,444
Bookmarked:76 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
15 votes for this article.
Popularity: 5.50 Rating: 4.68 out of 5
1 vote, 6.7%
1

2

3
2 votes, 13.3%
4
12 votes, 80.0%
5
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


Member

Occupation: Web Developer
Location: Australia Australia

Other popular Internet / Network articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
GeneralThis component has been superseded by HttpDownloaderControl PinmemberAlex_115:17 15 Jul '08  
Generalquestion Pinmemberjkersch1:08 24 Sep '07  
GeneralCool PinmemberRunnerDude9:30 6 Jul '07  
GeneralLooks good PinmemberTmefjhf17:30 28 Jun '07  
GeneralRe: Looks good Pinmemberrohim.yasin3:04 4 Jul '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 23 Sep 2007
Editor: Deeksha Shenoy
Copyright 2007 by Alex_1
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project