Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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
 
	_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.
 

// 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 23-Jun-12 19:31pm
hardsoft3.5K
Edited 24-Jun-12 17:15pm
v4

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

  Print Answers RSS
0 OriginalGriff 330
1 Sergey Alexandrovich Kryukov 279
2 BillWoodruff 245
3 Afzaal Ahmad Zeeshan 215
4 CPallini 205
0 OriginalGriff 5,635
1 DamithSL 4,496
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 24 Jun 2012
Copyright © CodeProject, 1999-2014
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