Mikhail
Mikhail

Reputation: 3305

Why UploadProgressChanged in WebClient.UploadFileAsync work not correctly?

I'm upload file and get upload progress like this:

using (var wc = new WebClient())
{
        wc.UploadProgressChanged += FileUploadProgressChanged;
        wc.Headers.Add(HttpRequestHeader.ContentType, "image/png");
        wc.UploadFileAsync(new Uri(url), filePath);
}

...

private void FileUploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
{
        ProgressBarUpload.Value = e.ProgressPercentage;
}

But after 50% e.ProgressPercentage return -441850 and then immediately returns 100. Why is this happening?

Upvotes: 0

Views: 1493

Answers (1)

Mikhail
Mikhail

Reputation: 3305

My solution:

private void FileUploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
{
        ProgressBarUpload.Value = e.BytesSent * 100 / e.TotalBytesToSend;
}

I also found two questions similar to this, but I have not managed to solve the problem. But it can be useful to others:

WebClient UploadFileAsync strange behaviour in progress reporting (cause of the problem - problems with authorization)

Uploading HTTP progress tracking (cause of the problem - the third-party application)


Note. Аfter downloading the file we receive a response from the server, it would be better to display the download file is 95% and the remaining 5% leave to display the response from the server. And in the end after a successful download and response from the server, we will be 100%.

PS: In the code, I did not show it, just say to those who might need it.

Upvotes: 1

Related Questions