2

I am relatively new in C# development.

I am trying to implement a downloader using WebClient.

What I have done is this-

private void startDownloadFile(string url)
{
    try
    {
        downloadProgress.Value = 0;
        downloadProgress.Visibility = Visibility.Visible;

        WebClient webClient = new WebClient();

        //Update UI with download progress
        webClient.DownloadProgressChanged += wcDownloadingString;

        webClient.DownloadStringAsync(new Uri(url), downloadFileLocation);
    }
    catch (Exception e)
    {
        // Stop the timer.
        timer.Enabled = false;
        MessageBox.Show(e.ToString());
        bDownload.IsEnabled = true;
        downloadProgress.Visibility = Visibility.Hidden;
    }
}

private void wcDownloadingString(object sender, DownloadProgressChangedEventArgs e)
{
    downloadProgress.Value = e.ProgressPercentage;
}

SO, I am supposed to get the UI update in progress bar, but I am not gettin that, because e.ProgressPercentage is returning 0 always.

It is returning 0 because, WebClient.TotalBytesToReceive is returning -1 always.

enter image description here

My complete code can be found in here-

https://github.com/abrarjahin/wpf-Download-Manager-With-Async-Task

Can anyone please help?

Re-

I have gone this places-

  1. https://social.msdn.microsoft.com/Forums/en-us/f5fe49aa-65ba-4ef9-a3b9-5ee6a2242829/why-totalbytestoreceive-is-always-1?forum=netfxnetcom
  2. c# WebClient DownloadProgresschanged TotalBytesToReceive = -1
  3. Download file from FTP with Progress - TotalBytesToReceive is always -1?

But can't find any solution from there.

Re Re-

And also, server is returning the file size always, because I am implementing a web source downloader, and it is not working anywhere, so not a problem with the server.

Community
  • 1
  • 1
Abrar Jahin
  • 13,970
  • 24
  • 112
  • 161
  • Are you sure the server is advertising the TotalBytesToReceive? have you checked with a network sniffer such as Wireshark? – o_weisman Nov 23 '16 at 12:05
  • Can you please go to - https://github.com/abrarjahin/wpf-Download-Manager-With-Async-Task and check the code? I am downloading Source Code from URL – Abrar Jahin Nov 23 '16 at 12:25

0 Answers0