Wednesday 2 November 2011

Download File Asynchronously

Questions Received:
          How to download a file asynchronously using C sharp Client.



Solution:
      Here is the business logic for download a file from website URL,

        private void btnDownload_Click(object sender, EventArgs e)
        {
            WebClient webClient = new WebClient();
            webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
            webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
            webClient.DownloadFileAsync(new Uri("http://education.vnu.edu.vn/eng/pic_upload/1252901905~Master_Mathematics.doc"), @"d:\Files.doc");
        }

        private void ProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
        {
            progressBar.Value = e.ProgressPercentage;
        }

        private void Completed(object sender, AsyncCompletedEventArgs e)
        {
            MessageBox.Show("Download completed!");
        }

[Note:] The Namespaces are,
                            using System.Deployment.Application;
                            using System.Net;

No comments:

Post a Comment