![]() |
Platforms, Frameworks & Libraries »
.NET Framework »
General
Beginner
License: The Code Project Open License (CPOL)
Windows Communication, Web Client Asynchronous File DownloaderBy Macari VeaceslavThe simplest way to download a resource using WebClient class |
C# (C# 1.0, C# 2.0, C# 3.0), .NET (.NET 3.0, .NET 3.5), WinForms, CEO, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
This project is intended to show how simple it is to implement an asynchronous downloader of an unlimited number of files, using the WebClient class. The key features are implemented using DownloadFileAsync and few callback functions which report progress information about active download.
The project does not use any additional threads. Everything is implemented using asynchronous callback methods which are called by the WebClient object when the internal state of the downloader changes.
First of all, look at the program window when some parallel downloads are in progress. It’s possible to add as many download tasks as you want.
Initially, a main process is created which brings up to the user the main window with a input field for URI, an add button which starts the download process, and listview which is used to display status (in real time) of each WebClient (Figure 3). After the user presses the Add button ([+]), the DownloaderTask object creates a new downloader and assigns to it Progress Callback and End Callback events.
The download is started in the same turn. The resource is saved to the current folder and the name is automatically extracted from the last segment of the URI:
fileName = this.uriData.Segments[this.uriData.Segments.Length - 1];
When the user presses the add button, a DownloaderTask object is created and added to the dwnTasks list (Figure 4):
dwnTasks.Add(new DownloaderTask(tbUri.Text, lvDownloads));
The first parameter represents the URI and the second is a listview, where the download task object itself creates an informational group for the downloaded file and continuously updates it. The DownloaderTask constructor will create a WebClient() object, then will assign callbacks:
webClient.DownloadProgressChanged +=
new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
webClient.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler
(webClient_DownloadFileCompleted);
Also the constructor of the DownloaderTask object creates a group in passed listview (if not null) which will describe the current status of the downloadable file and the operation result at the end (See figure 2).
There are, of course, some TO DO items like:
| You must Sign In to use this message board. | |||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 27 May 2008 Editor: Deeksha Shenoy |
Copyright 2008 by Macari Veaceslav Everything else Copyright © CodeProject, 1999-2009 Web18 | Advertise on the Code Project |