Click here to Skip to main content
15,894,291 members
Please Sign up or sign in to vote.
4.00/5 (1 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

C#
_WebClient.OpenRead(uri);
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.


C#
// 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)
        {
            currentitem++;
            DownloadNextFile();
        }
    }

    // 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;
        DownloadNextFile();
    }

    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.

        _WebClient.OpenRead(uri);
        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
Updated 24-Jun-12 16:15pm
v4

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900