|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionFor one of my projects at work, I needed the ability to download a file from a given URL. Unfortunately, the methods provided by the As I started my searches on the web for any existing code, I came across two articles on CodeProject that helped get me very close to what I needed. The first article was by John Batte, while the second article by Phil Crosby was a derivation of his work. Using both of these articles as a starting point, I was able to make another derivation that accomplished my goals. The code samples and content of this article are focused on the .NET 2.0 implementation and any code updates have been made only to that version. The .NET 1.1 implementation is provided for backwards compatibility only and I have no further plans to continue making changes to it. Using the CodeThe private void DoDownload()
{
FileDownloader downloader = new FileDownloader();
downloader.Download(new Uri("http://www.codeproject.com" +
"/info/stuff/cp_bg_black1024.gif"), @"C:\Temp");
}
This creates a new instance of the
This internal method, called
Once the While this implementation is functional, it does prevent anyone from using the class who is behind a proxy or who requires authentication to access the requested URL. In order to support these scenarios, the Finally, the events provide progress feedback. The
These events are raised during the actual file download to provide the caller with status information ( private void DoDownload()
{
Uri fileUrl = new Uri("http://www.codeproject.com" +
"/info/stuff/cp_bg_black1024.gif");
string tempPath = Path.GetTempPath();
string fileName = Path.GetFileName(fileUrl.AbsoluteUri);
string downloadPath = Path.Combine(tempPath, fileName);
FileDownloader downloader = new FileDownloader();
downloader.Proxy = WebRequest.DefaultWebProxy;
downloader.Credentials = CredentialCache.DefaultCredentials;
downloader.DownloadCompleted += new
EventHandler<FileDownloadCompletedEventArgs>(OnDownloadCompleted);
downloader.DownloadProgressChanged += new
EventHandler<FileDownloadProgressChangedEventArgs>(
OnDownloadProgressChanged);
downloader.DownloadStatusChanged += new
EventHandler<FileDownloadStatusChangedEventArgs>(
OnDownloadStatusChanged);
// Download the requested file to the specified folder
downloader.Download(fileUrl, tempPath);
}
private void OnDownloadCompleted(object sender,
FileDownloadCompletedEventArgs e)
{
// Do something when the download completes.
}
private void OnDownloadProgressChanged(object sender,
FileDownloadProgressChangedEventArgs e)
{
// Do something when the progress changes,
// usually you would increment a progress bar.
}
private void OnDownloadStatusChanged(object sender,
FileDownloadStatusChangedEventArgs e)
{
// Do something when that status changes.
}
Here is the same example using VB.NET. This example is based on the comment [^] by CBlackburn [^]. Private Sub DoDownload()
Dim fileUrl As Uri = New Uri("http://www.codeproject.com/" & _
"info/stuff/cp_bg_black1024.gif")
Dim tempPath As String = Path.GetTempPath()
Dim fileName As String = Path.GetFileName(fileUrl.AbsoluteUri)
Dim downloadPath As String = Path.Combine(tempPath, fileName)
Dim downloader As FileDownloader = New FileDownloader()
downloader.Proxy = WebRequest.DefaultWebProxy
downloader.Credentials = CredentialCache.DefaultCredentials
AddHandler downloader.DownloadCompleted, _
AddressOf OnDownloadCompleted
AddHandler downloader.DownloadProgressChanged, _
AddressOf OnDownloadProgressChanged
AddHandler downloader.DownloadStatusChanged, _
AddressOf OnDownloadStatusChanged
downloader.Download(fileUrl, tempPath)
End Sub
Private Sub OnDownloadCompleted(ByVal sender As Object, _
ByVal e As FileDownloadCompletedEventArgs)
' Do something when the download completes.
End Sub
Private Sub OnDownloadProgressChanged(ByVal sender As Object, _
ByVal e As FileDownloadProgressChangedEventArgs)
' Do something when the progress changes,
' usually you would increment a progress bar.
End Sub
Private Sub OnDownloadStatusChanged(ByVal sender As Object, _
ByVal e As FileDownloadStatusChangedEventArgs)
' Do something when that status changes.
End Sub
The complete public API for the Points of InterestWhile the The Revision History16-August-2007:
12-August-2007:
11-January-2007:
27-August-2006:
25-August-2006:
|
||||||||||||||||||||||