![]() |
General Programming »
Internet / Network »
HTTP
Intermediate
License: The Code Project Open License (CPOL)
HTTP File Downloader Class for .NETBy Alex_1HTTP File Downloader Class for .NET (HTTPFileDownloader) |
C#, Windows, .NET, Visual Studio, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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.
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.
If a newer version of this component is released, it can be found here along with other useful classes and components.
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
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 Web19 | Advertise on the Code Project |