C# Programming > Web

Download File Progress

The application is now ready to download files from the internet. But you have probably noticed that whenever a program downloads data from the internet, it is useful to display a progressbar.

How can do we do this with our code?

The simpliest way would be something like this:

progressBar1.Maximum = (int)response.ContentLength;
progressBar1.Value = 0;
while (stream.CanRead)
    byte nextByte = stream.ReadByte();

In this case we simply update a ProgressBar object every time we download a new byte of data. The code is simple and straightforward and we might be tempted to leave it like this. It would certainly work in a desktop application.

But we have to remember that we are downloading data from the internet. Every read operation is fairly costly in terms of time and network activity. Thus reading one byte at a time is very inefficient. Ideally we would want to read the entire data all at once, but then we wouldn't be able to keep track of the progress. (And that would actually not be very reliable anyway.)

The solution is to download small chunks of data at a time. A popular size is 1 kilobyte chunks (1024 bytes). We will use this in our application since it is a good balance to keep track of the downloaded data without wasting too much time requesting more data.

So a single read operation would look more like this:

byte[] buffer = new byte[1024];
int numBytesRead = stream.Read(buffer, 0, buffer.Length);
progressBar1.Value += numBytesRead;

The only tricky part is to notice that we can't guarantee that the stream will return exactly 1024 bytes (since the total size of the file is probably not evenly divisible into 1KB blocks). But this is not a problem since the Read function returns the number of bytes that were actually read. This also gives us a way of knowing when there is nothing left to read (when the number of bytes read is 0 that means that there is no more to read).

The Source Code

The sample code below shows a basic C# application that can download files from the internet, with a progress bar and everything. Files in the application are written to memory first and then optionally to disk, which shows off a bit of how flexible this whole thing really is.

Also note that this is a single-threaded application. One can imagine splitting the download logic and progress-updating logic into separate threads. I leave that as an exercise for the reader.

Page 1
<< Downloading a File from a URL

Back to C# Article List