Click here to Skip to main content
11,415,046 members (66,193 online)
Click here to Skip to main content

Downloader Component

, 14 Jan 2007
Rate this:
Please Sign up or sign in to vote.
A component to download files over the network with support for proxies, SSL and resume.
Sample Image - Downloader.jpg

Introduction

Downloading files over the network could be a cumbersome task. Trying to implement other features like auto-resume and support for proxy servers, etc. makes it even harder. Using this component can make things a lot easier for yourself. You can download files over proxies, password protected sites or untrusted SSL connections simply by setting a few properties. The example that is provided shows how to do the job and how to handle events over a simple GUI.

Since the download operation takes place in a worker thread asynchronously, you don't need to worry about downloading large files or blocking the user interface. There are some points you need to have in mind though.

Aborting the Worker Thread

Stopping a download operation that is running is tricky. You could just call the DownloadThread.Abort() method on the downloader thread to stop the work, but this is not the recommended way. The main reason for this is that Thread.Abort throws an exception which can occur at any time during your work and leaves your running state inconsistent. The easier way is to check for a boolean field, and terminate the operation when the field is set, or run the job on an entirely different process. To read more about this idea, check Ian Griffiths blog post here.

Updating GUI

When running a download job, the Downloader component provides its internal progress and error state through events and custom eventargs. Since the eventargs are constructed on the worker thread, trying to update the user-interface from the worker thread throws a Cross-Thread exception. The workaround to update the user interface is to use the Invoke method (inherited from Control), a Delegate and call to the necessary method(s), like this:

private delegate void ParamMethodInvoker
    (long fileSize, long progressValue, string message);

/// <summary>
/// Call UpdateProgress method using a delegate and provide method parameters
/// </summary>
private void OnDownloadProgressChanged
    (object sender, DownloadProgressEventArgs e)
{
    Invoke(new ParamMethodInvoker(UpdateProgress), new object[] 
        { e.BytesRead, e.TotalBytes, e.Message });
}

/// <summary>
/// Normally update the user interface
/// </summary>
private void UpdateProgress(long bytesRead, long totalBytes, string message)
{
    int kbRead = Convert.ToInt32(bytesRead) / 1024;
    int kbTotal = Convert.ToInt32(totalBytes) / 1024;
    int percent = Convert.ToInt32((bytesRead * 100) / totalBytes);

    progress.Value = percent;
    lblDownloadMessage.Text = 
        string.Format("{0:#,###} of {1:#,###} KBytes ({2}%)", 
        kbRead, kbTotal, percent);
}

Resuming a Download

To resume a download job, we can simply continue where we left off. When restarting a download job, the downloader component first checks for the existing file at the specified location (through DownloadPath property). If the file exists, it reads the Length of the existing file and sets the HttpWebRequest's range using AddRange method. Overloads of the AddRange method could be used to handle multi-part downloading of a single file.

long startingPoint = 0;

if(File.Exists(DownloadPath))
{
    startingPoint = new FileInfo(DownloadPath).Length;
}

HttpWebRequest _Request = (HttpWebRequest)HttpWebRequest.Create(url);
_Request.AddRange(startingPoint);

Points of Interest

Implementing other features such as multi-part downloading (like download accelerator applications) and FTP downloads seems like a good idea. Any suggestions/comments/feedback are highly appreciated.

History

Version 1

  • Provided the component with base features and events
  • Provided a basic form to show features of the component

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Hadi Eskandari
Software Developer (Senior) Readify
Australia Australia
Working on both Java and .NET Technology, I have developed various enterprise level applications on both platforms. Currently, I am working as a Senior Software Developer at Readify which is a leading company on .NET technology in Australia.
Follow on   Twitter

Comments and Discussions

 
GeneralMy vote of 5 Pin
Michael Haephrati7-Mar-13 12:03
mvpMichael Haephrati7-Mar-13 12:03 
QuestionMy vote of 5 Pin
Pouriya.GH30-Apr-12 4:27
memberPouriya.GH30-Apr-12 4:27 
GeneralMy vote of 5 Pin
Shahin Khorshidnia3-Apr-11 20:18
memberShahin Khorshidnia3-Apr-11 20:18 
GeneralError Downloading file greater than 2GB [modified] Pin
akshayoh12-Feb-11 9:43
memberakshayoh12-Feb-11 9:43 
GeneralRe: Error Downloading file greater than 2GB [modified] Pin
KOS_void28-Feb-12 5:39
memberKOS_void28-Feb-12 5:39 
Generaldoc required Pin
prudhvi1234516-Mar-10 10:03
memberprudhvi1234516-Mar-10 10:03 
GeneralImplementing Speed Limiting (aka Throttling/Shaping) Pin
TrendyTim14-Sep-07 17:30
memberTrendyTim14-Sep-07 17:30 
Generalpoint of interest Pin
jugomkd7510-Aug-07 9:25
memberjugomkd7510-Aug-07 9:25 
GeneralRe: point of interest Pin
jugomkd7510-Aug-07 22:20
memberjugomkd7510-Aug-07 22:20 
QuestionHow about FTP ?? Pin
_Thurein_9-May-07 18:09
member_Thurein_9-May-07 18:09 
AnswerRe: How about FTP ?? Pin
h.eskandari9-May-07 20:52
memberh.eskandari9-May-07 20:52 
GeneralRe: How about FTP ?? Pin
_Thurein_9-May-07 22:36
member_Thurein_9-May-07 22:36 
GeneralRe: How about FTP ?? Pin
h.eskandari9-May-07 23:41
memberh.eskandari9-May-07 23:41 
GeneralRe: How about FTP ?? Pin
Gaara [BTK]12-Oct-07 1:28
memberGaara [BTK]12-Oct-07 1:28 
Hi...I dont know if it is exactly what u meant...but i just created a FtpWebRequest,FtpWebResponse and a public bool FTPEnabled.

and then i changed the private void DownloadFile(string url, string path, long startPoint) method in the downloader.cs like that:

private void DownloadFile(string url, string path, long startPoint)
{
int startingPoint = Convert.ToInt32(startPoint);

try
{
//For using untrusted SSL Certificates
ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(OnCheckRemoteCallback);

if (FTPEnabled)
{
_FTPRequest = (FtpWebRequest)WebRequest.Create(url);
_FTPRequest.Method = WebRequestMethods.Ftp.DownloadFile;
//_FTPRequest.EnableSsl = true;
}
else
{
_Request = (HttpWebRequest)HttpWebRequest.Create(url);
_Request.AddRange(startingPoint);
}

if (!string.IsNullOrEmpty(LoginUsername))
{
if (FTPEnabled)
{
_FTPRequest.Credentials = new NetworkCredential(LoginUsername, LoginPassword);
}
else
_Request.Credentials = new NetworkCredential(LoginUsername, LoginPassword);
}
else
{
if (FTPEnabled)
{
//_FTPRequest.Credentials = CredentialCache.DefaultCredentials;
}
else
_Request.Credentials = CredentialCache.DefaultCredentials;
}
if (!FTPEnabled)
{
if (ProxyEnabled)
{
_Request.Proxy = new WebProxy(ProxyURL);
}
}

if (FTPEnabled)
{
// Get the ServicePoint object used for this request, and limit it to one connection.
// In a real-world application you might use the default number of connections (2),
// or select a value that works best for your application.

ServicePoint sp = _FTPRequest.ServicePoint;
sp.ConnectionLimit = 1;

_FTPResponse = (FtpWebResponse)_FTPRequest.GetResponse();
}
else
{
_Response = (HttpWebResponse)_Request.GetResponse();

}

Stream responseSteam;
if(FTPEnabled)
responseSteam = _FTPResponse.GetResponseStream();
else
responseSteam = _Response.GetResponseStream();

if (startingPoint == 0)
{
_SaveFileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
}
else
{
_SaveFileStream = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
}

int bytesSize;
long fileSize;

if(FTPEnabled)
fileSize = _FTPResponse.ContentLength;
else
fileSize = _Response.ContentLength;

byte[] downloadBuffer = new byte[DEF_PACKET_LENGTH];

OnProgressChanged(new DownloadProgressEventArgs(fileSize, 0, "Starting the download..."));

while ((bytesSize = responseSteam.Read(downloadBuffer, 0, downloadBuffer.Length)) > 0 && !_ExitThread)
{
if (_ExitThread)
{
break;
}

_SaveFileStream.Write(downloadBuffer, 0, bytesSize);
OnProgressChanged(new DownloadProgressEventArgs(_SaveFileStream.Length, fileSize + startingPoint, "Download in progress..."));
}

if (_ExitThread)
{
_SaveFileStream.Close();
_SaveFileStream.Dispose();
OnDownloadCompleted(new DownloadCompleteEventArgs(false, true));

return;
}

OnDownloadCompleted(new DownloadCompleteEventArgs(true, false));
}
catch (Exception ex)
{
OnDownloadError(new DownloadErrorEventArgs(ex, string.Format("Problem downloading and copying file from {0} to {1}.", url, path)));
OnDownloadCompleted(new DownloadCompleteEventArgs(false, false));
}
finally
{
if (_SaveFileStream != null)
{
_SaveFileStream.Close();
_SaveFileStream.Dispose();
}

if (_Response != null)
{
_Response.Close();
}
if (_FTPResponse != null)
{
_FTPResponse.Close();
}
}
}

I tried it and it worked very well.Dont forget to switch the mean settings in the beginning -> DownloadForm.cs

if (cbUserFTP.Checked)
{
downloader.LoginUsername = txtUsername.Text;
downloader.LoginPassword = txtPassword.Text;
downloader.FTPEnabled = true;
}

Questionwhat if from a webdav server Pin
roychoo16-Mar-07 5:11
memberroychoo16-Mar-07 5:11 
AnswerRe: what if from a webdav server Pin
Hadi Eskandari16-Mar-07 19:51
memberHadi Eskandari16-Mar-07 19:51 
GeneralTest for file size if resuming Pin
Jeffrey Scott Flesher23-Jan-07 19:14
memberJeffrey Scott Flesher23-Jan-07 19:14 
GeneralVery similar... Pin
Scott Dorman15-Jan-07 7:30
memberScott Dorman15-Jan-07 7:30 
GeneralRe: Very similar... Pin
Hadi Eskandari16-Jan-07 20:22
memberHadi Eskandari16-Jan-07 20:22 
GeneralRe: Very similar... Pin
Scott Dorman18-Jan-07 5:09
memberScott Dorman18-Jan-07 5:09 
GeneralTnx For This Article Pin
yunas14-Jan-07 3:36
memberyunas14-Jan-07 3:36 
GeneralJust a little problem Pin
Vertyg014-Jan-07 2:34
memberVertyg014-Jan-07 2:34 
GeneralRe: Just a little problem Pin
Hadi Eskandari14-Jan-07 20:14
memberHadi Eskandari14-Jan-07 20:14 
GeneralRe: Just a little problem Pin
Hadi Eskandari11-Feb-07 21:50
memberHadi Eskandari11-Feb-07 21:50 
GeneralRe: Just a little problem Pin
Vertyg011-Feb-07 22:42
memberVertyg011-Feb-07 22:42 
GeneralRe: Just a little problem Pin
Hadi Eskandari12-Feb-07 20:39
memberHadi Eskandari12-Feb-07 20:39 
GeneralRe: Just a little problem Pin
Vertyg012-Feb-07 21:13
memberVertyg012-Feb-07 21:13 
GeneralRe: Just a little problem Pin
deerchao8-Aug-07 23:38
memberdeerchao8-Aug-07 23:38 

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
Web04 | 2.8.150427.4 | Last Updated 14 Jan 2007
Article Copyright 2007 by Hadi Eskandari
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid