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.
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-
- https://social.msdn.microsoft.com/Forums/en-us/f5fe49aa-65ba-4ef9-a3b9-5ee6a2242829/why-totalbytestoreceive-is-always-1?forum=netfxnetcom
- c# WebClient DownloadProgresschanged TotalBytesToReceive = -1
- 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.