How to build a basic file downloader that displays download progress
This article describes an All-In-One Code Framework sample that is available for download. This code sample demonstrates how to build a basic file downloader that shows the progress of the download.
The System.Net.WebClient class in this code sample has a DownloadProgressChanged event. You can register this event to show download progress. However, the System.Net.WebClient class does not support the pause and resume functions.
This code sample could be used to download data from an internet. Also, this code sample supports the following features:
- Download a specified block data for the whole file.
- Start, pause, resume and cancel a download.
- Set the buffer and cache size.
- Show the progress during the download.
Download informationTo download this code sample, click one of the following links:
Download a specific block of data from a fileTo download a specific block of data, use the AddRange method of the System.Net.HttpWebRequest class to add a byte range header to a request. You can use the following syntax to call the AddRange method:
Public void AddRange (int from, int to)
Note To do this, the server must support the Accept-Ranges header.
Pause and resume a downloadThe HttpDownloadClient class can be used to start, pause, resume and cancel a download. If the download function is paused, the number of bytes that have been downloaded is stored. If the download function is resumed, the file will resume downloading from the same location in which the download was paused.
Set the buffer and cache sizeAfter you set the buffer and cache size, the downloaded data is stored in a MemoryStream first, and then written to a local file.
Show the progress during the downloadWhen a specified number of bytes of data is downloaded, the HttpDownloadClient class fires a DownloadProgressChanged event. If the download is completed or canceled, the class will also fire a DownloadCompleted event. The MainForm class registers these two events and shows the download progress.
Note For more information about how to create and deploy the sample application, see the Readme.txt file that is included in the download package.
LanguagesThis code sample is available in the following programming languages:
PrerequisitesThis sample application was created using Microsoft Visual Studio 2010 and Microsoft .NET Framework 4.
ReferencesFor more information about the HttpWebRequest class, visit the following Microsoft Developer (MSDN) website: AddRange method, visit the following MSDN website:
What is All-In-One Code Framework? All-In-One Code Framework shows most Microsoft development techniques by using code samples in different programming languages. Each example is carefully selected, composed, and documented to show one common code scenario. For more information about All-In-One Code Framework, visit the following Microsoft website:
How to find more All-In-One Code Framework samplesTo find more All-In-One Code Framework samples, search for "kbcodefx" together with related keywords on the Microsoft support Web site. Or, visit the following Microsoft website:
Rapid publishing disclaimerMicrosoft corporation and/or its respective suppliers make no representations about the suitability, reliability, or accuracy of the information and related graphics contained herein. All such information and related graphics are provided "as is" without warranty of any kind. Microsoft and/or its respective suppliers hereby disclaim all warranties and conditions with regard to this information and related graphics, including all implied warranties and conditions of merchantability, fitness for a particular purpose, workmanlike effort, title and non-infringement. You specifically agree that in no event shall Microsoft and/or its suppliers be liable for any direct, indirect, punitive, incidental, special, consequential damages or any damages whatsoever including, without limitation, damages for loss of use, data or profits, arising out of or in any way connected with the use of or inability to use the information and related graphics contained herein, whether based on contract, tort, negligence, strict liability or otherwise, even if Microsoft or any of its suppliers has been advised of the possibility of damages.
Article ID: 2512243 - Last Review: 03/10/2011 03:27:00 - Revision: 1.0
- kbcodefx kbinfo kbnomt kbrapidpub atdownload kbsurveynew KB2512243