Download File Synchronously
using System.Net;
WebClient webClient = new WebClient();
webClient.DownloadFile("http://webxperts.com/myfile.pdf", @"c:\myfile.pdf");
Download File Asynchronously
You can download file without blocking the main thread use asynchronous method DownloadFileAsync. You can also set event handlers to
show progress and to detect that the file is
downloaded.
private void btnDownloadFiles_Click(object sender, EventArgs e)
{
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
webClient.DownloadFileAsync(new Uri("http://webxperts.com/myfile.pdf"), @"c:\myfile.pdf");
}
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
}
private void Completed(object sender, AsyncCompletedEventArgs e)
{
Response.Write("Download completed!");
}
No comments:
Post a Comment