4

I've had a look around the web and can't see anyone else with this problem. I'm using a web client using DownloadFileAsync and when the event handler (DownloadProgressChanged) is called the TotalBytesToReceive (from the DownloadProgressChangedArgs) is equalling -1 for some reason, thus stopping me from being able to use a progress bar. I'm detecting <0 and if so just guessing 100meg for now, to get round it.

The BytesRecieved is working however, and the file is actually being downloaded, and the AsynCompletedEventHadnler seems to be getting called so it knows its finished (so must know the TotalBytesToReceive somehow?). I'm using a WebClient with credentials and proxy credentials to download from a password protected external site going through an internal network (so needed both) - not sure if that would make any difference.

I was previously using WebClient.DownloadData getting the byte data and saving it separately and putting it in a background worker,and it worked fine (if quite slow) but there was no way I could show progress this way. Also the DownloadFileAsync seems to do all of this for me so saved a lot of code.

    private void DLFile_AsyncWithStatus(string DLlocation, string un, string pw, string destLoc)
    {
        WebClient wc = new WebClient();
        wc.Credentials = new NetworkCredential(un, pw); // website login
        wc.Proxy.Credentials = new NetworkCredential(ProxyUsername, ProxyPassword, ProxyDomain); //proxy login

        Uri uri = new Uri(DLlocation);

        // Specify that the DownloadFileCallback method gets called when the download completes.
        wc.DownloadFileCompleted += new AsyncCompletedEventHandler(DLFile_AsynWithStatus_Completed);
        // Specify a progress notification handler.
        wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);
        try
        {
            wc.DownloadFileAsync(uri, destLoc);

        }
        catch (WebException e)
        {
            MessageBox.Show(e.Message);
        }

    }
    private void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e)
    {

        double bytesIn = double.Parse(e.BytesReceived.ToString());
        double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
        if (totalBytes < 0) { 
            totalBytes = 100.0 * 1000000.0; //guess 100 meg since it is not detecting total bytes
        } 
        double percentage = bytesIn / totalBytes * 100;


        lblTmpStatus.Text = "Downloaded " + e.BytesReceived + " of " + e.TotalBytesToReceive;

        progressBar1.Value = int.Parse(Math.Truncate(percentage).ToString());
    }
    void DLFile_AsynWithStatus_Completed(object sender, AsyncCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            Msg(e.Error.Message);
        }
        else
        {
            progressBar1.Value = 100;//temp.. finish it off incase was less than 100 meg.
        }
    }

labrys
  • 129
  • 1
  • 7
  • 2
    It's probably because the server doesn't return actual size of the file in the header of response. Have you tried to download a file from other resource ? – IL_Agent Nov 06 '14 at 12:08
  • I think you might be right, just tried picking a random image instead and that did show a size. Thing is the place I am trying to automate the download from looks to be a little like a ftp site (shows all files and sizes. So not sure why my program could not see that from the server / why server would not pass these details). Will have to try several combinations (other files on that server / images or something, in and outside of its secure area). Need to narrow it down. – labrys Nov 10 '14 at 11:32
  • Just tried downloading an image from that server and it did tell my program the filesize. I'm wondering if its just password protected areas that do not pass the filseize for some reason? – labrys Nov 10 '14 at 13:54
  • I have the same problem with you. It works fine with jpg or png files, only return -1 for totalBytesToReceive for videos/audios. I haven't found a way to fix it yet. – Chandler Jan 18 '19 at 02:06

0 Answers0