Page 1 of 1

Download File Asynchronously with ProgressBar This tutorial will show how to download a file asychronously and use a

#1 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Posted 18 July 2009 - 08:47 AM

If you've ever wanted to create your own Download Manager, where you download a file, keep track of the amount of the file that has been downloaded, and use a ProgressBar, this is the tutorial for you.

So first, drag a ProgressBar and a Button from the Toolbox onto the form.

Create a Click_Event for the button by double clicking it.

Add this using statement to the top of the form.
using System.Net;



In the Click_Event, we are going to create a WebClient object to download the file.
private void btnStartDownload_Click(object sender, EventArgs e)
{
    WebClient client = new WebClient();
    client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
    client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
    
    // Starts the download
    client.DownloadFileAsync(new Uri("SomeURLToFile"), "SomePlaceOnLocalHardDrive");

    btnStartDownload.Text = "Download In Process";
    btnStartDownload.Enabled = false;
}



In the previous code, we are creating two event handlers to handle when the Progress of the file download changes, and when the download completes.

We also use the DownloadFileAsync method so that the download(which could be lengthy) does not freeze the main GUI.

Next, we need to do the code to calculate the percentage of the download complete so that we can assign it to the ProgressBar.
void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
    double bytesIn = double.Parse(e.BytesReceived.ToString());
    double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
    double percentage = bytesIn / totalBytes * 100;

    progressBar1.Value = int.Parse(Math.Truncate(percentage).ToString());
}



In this code, we use the BytesReceived and TotalBytesToReceive properties of the DownloadProgressChangedEventArgs object.

Next, we need to do the code for the event when the download completes.
void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
    MessageBox.Show("Download Completed");

    btnStartDownload.Text = "Start Download";
    btnStartDownload.Enabled = true;
}



That is all the code you need.

If you would like to have the percentage "embedded" into the ProgressBar, check out JacobJordan's tutorial here, http://www.dreaminco...wtopic94631.htm

Is This A Good Question/Topic? 3
  • +

Replies To: Download File Asynchronously with ProgressBar

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Posted 18 July 2009 - 09:22 AM

Nice tutorial, way to go :)
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Posted 18 July 2009 - 10:07 AM

Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 virtyaluk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-February 11

Posted 18 February 2011 - 04:21 AM

Very good, Thanks!
Was This Post Helpful? 0
  • +
  • -

#5 bishop.dustin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-February 11

Posted 23 February 2011 - 10:32 AM

Hey sorry if I'm being stupid but at the end result should the file from url that you specified should be in the folder on the local dive. Cause my progress bar work but no file in destination.
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Posted 01 March 2011 - 05:39 AM

View Postbishop.dustin, on 23 February 2011 - 01:32 PM, said:

Hey sorry if I'm being stupid but at the end result should the file from url that you specified should be in the folder on the local dive. Cause my progress bar work but no file in destination.


Yes, it should be in the folder.
Was This Post Helpful? 0
  • +
  • -

#7 aleale97  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-August 11

Posted 03 August 2011 - 02:49 AM

Hi...
It give me an error in this line:
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);

I could not find out why, the error is this:
The name 'client_DownloadProgressChanged' does not exist in the current contest

I found this error when I declared a variable in an if and then tried to use it outside. But in thi contest i can't find out why...
Thanks for the guide overall...
Was This Post Helpful? 0
  • +
  • -

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Posted 03 August 2011 - 04:19 AM

View Postaleale97, on 03 August 2011 - 05:49 AM, said:

Hi...
It give me an error in this line:
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);

I could not find out why, the error is this:
The name 'client_DownloadProgressChanged' does not exist in the current contest

I found this error when I declared a variable in an if and then tried to use it outside. But in thi contest i can't find out why...
Thanks for the guide overall...


The method that it will use when the progress changes(client_DownloadProgressChanged) doesn't exist. You must have missed it. You can also use IntelliSense to help you out. When you type "+=", IntelliSense will try to complete it for you. If you simply hit [TAB] [TAB], it will create the rest of the event handler and create the method stub for you.
Was This Post Helpful? 0
  • +
  • -

#9 mitultechs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-October 11

Posted 18 October 2011 - 05:06 AM

Amazing information. Thanks
Was This Post Helpful? 0
  • +
  • -

#10 reni90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-September 13

Posted 03 September 2013 - 03:53 AM

Thanks for the question but i have one problem the line
double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());

Always returns -1 :S and then i get an error
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1