0

I have run into some problems using C# WebClient DownloadFileAsync and hope that you would help me.

Here's my scenario:

I am downloading many files at the same time (http://example.com/abc/1.jpg, http://example.com/abc/efg/2.jpg, etc.) using WebClient DownloadFileAsync.

My current example code is:

while (theres still files in http://example.com/abc/) {
    // filename will be something like /abc/1.jpg, /abc/efg/2.jpg
    if (!file.exists(directory.getcurrentdirectory()+filename.Replace('/', '\\'))) {
        WebClient webClient = new WebClient();
        webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(downloadProgressChanged);
        webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(downloadCompleted);
        webClient.DownloadFileAsync(new Uri("http://example.com"+filename), Directory.GetCurrentDirectory()+filename.Replace('/', '\\'));
    }

How do I make it so that all the files being downloaded is shown in one progress bar?

eg. 1.jpg is 50kb, 2.jpg is 50kb, when 1.jpg completes download the progress bar will show 50%, and 2.jpg will be from 51% to 100% in the progress bar.

Also if the filename is /abc/1.jpg, if my current directory do not have a folder called abc, the download will not work. How do I make it create the folder automatically depending on filename? (eg. /random123/3.jpg, /anotherrandom/4.jpg, etc.)

initialshl
  • 53
  • 2
  • 9
  • You're asking two unrelated things, you should ask two questions. – svick Jun 19 '11 at 14:08
  • Do you know the size of all images before you start downloading? Or should the progress bar be independent of the size of the images? – svick Jun 19 '11 at 14:09
  • I do not know the total size of all images before I start the download. It should be dynamic. – initialshl Jun 19 '11 at 14:58
  • Well, what should be displayed if there are two images: I already finished downloading of one image with the size of 50 kB, but I don't know the size of the other one yet? – svick Jun 19 '11 at 15:01
  • Is there any way to calculate the total bytes I have downloaded so far? from my code above. – initialshl Jun 19 '11 at 17:50

2 Answers2

0

To give an overall progress counter for you will need a centralised routine that gathers the amount-completed-so-far and total-amount-to-download for each of the in-progress transfers and sums them to calculate the overall totals. Because the transfers are running in parallel you can't just assign a chunk of the progress bar (e.g 51-100%) to each file.

With most APIs you will need to create folders for yourself before writing files into them. Just do this to create all missing folders on the path before you start writing the file:

Directory.CreateDirectory(Path.GetDirectoryName(filename));
Jason Williams
  • 56,972
  • 11
  • 108
  • 137
  • Thanks! The directory creation now works! Is there any way to gather the total size of all files that will be downloaded, and calculate the percentage myself, and make it into one progress bar? – initialshl Jun 19 '11 at 14:39
  • Can you help me with the total amount-completed-so-far for the files being downloaded? I am able to get the total size of files to be downloaded from php, but I do not know how to calculate the progress bar. – initialshl Jun 19 '11 at 17:48
0

To do the progress bar thing, you can use a queue. When something finishes downloading, update the progress bar, and start the next download. However, I don't think the WebClient class can tell you how much of the download finished, only if it is finished or not.

To verify if the directory exists, use:

if (!System.IO.Directory.Exists(folder_name)) System.IO.Directory.Create(folder_name);

You can get the directory from a path, using the System.IO.Path class.

Tibi
  • 4,015
  • 8
  • 40
  • 64