Click here to Skip to main content
13,090,678 members (44,631 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I am trying to download several files from those selected in a list view.
The download works ok for a single files but fails when I select more than on file. I only ever can get the first file to download and show the progress bar.

I know that my code is providing the correct uri because of the line
lbl_DownloadFile.Text = uri + " - Bytes " + bytes_total.ToString() displays the correct file but for some reason _WebClient.DownloadFile(new Uri(uri), destinationfile) gets the file with zero bytes and never downloads.

Any help where i am going wroung would be appeaciated.

Issue found in this code

Int64 bytes_total= Convert.ToInt64(_WebClient.ResponseHeaders["Content-Length"]);
lbl_DownloadFile.Text = uri + " - Bytes " + bytes_total.ToString() ;

The web client is remaining open will try a procedure to get the above data.

// Occurs when an asynchronous file download operation completes.
    private void _DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        // File download completed
        pb_Download.Enabled = Enabled;
        lbl_DownloadFile.Text = "Download completed";
        if (currentitem < checkedItems.Count)
    // Occurs when an asynchronous download operation successfully transfers some or all of the data.
    private void _DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
        // Update progress bar
        progressBar1.Value = e.ProgressPercentage;

    // download button click event
    void Pb_DownloadClick(object sender, EventArgs e)
        _WebClient = new System.Net.WebClient();
        _WebClient.Credentials = new System.Net.NetworkCredential(this.tb_Username.Text.Trim(),this.tb_Password.Text.Trim());
        _WebClient.Headers.Add("Accept-Encoding", "");
        checkedItems = lv_Materials.CheckedItems;
    void DownloadNextFile()
        ListViewItem item = checkedItems[currentitem];
        DownloadFile(this.tb_WebPage.Text + item.Tag.ToString(), "");
    void DownloadFile(string uri, string destinationfile)
        string pathDownload = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        if (String.IsNullOrEmpty(destinationfile))
            destinationfile = Path.Combine(pathDownload,uri.Substring(uri.LastIndexOf("/")+1));

        // Disable download button to avoid clicking again while downloading the file
        pb_Download.Enabled = false;
        // Downloads, to a local file, the resource with the specified URI.
        // This method does not block the calling thread.
        Int64 bytes_total= Convert.ToInt64(_WebClient.ResponseHeaders["Content-Length"]);
        lbl_DownloadFile.Text = uri + " - Bytes " + bytes_total.ToString() ;
        this.progressBar1.Value = 0;
        _WebClient.DownloadFileCompleted += new AsyncCompletedEventHandler(_DownloadFileCompleted);
        _WebClient.DownloadProgressChanged += new System.Net.DownloadProgressChangedEventHandler(_DownloadProgressChanged);
//      MessageBox.Show(uri);
        _WebClient.DownloadFileAsync(new Uri(uri), destinationfile);
//      _WebClient.DownloadFile(new Uri(uri), destinationfile);
        // TODO: check file size after download
Posted 23-Jun-12 18:31pm
Updated 24-Jun-12 16:15pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170813.1 | Last Updated 24 Jun 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100