Click here to Skip to main content
15,896,343 members

Use of System.Net.WebClient stalls on 2nd download

hardsoft asked:

Open original thread
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
    }

}
Tags: C#

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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