0

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?

Mikhail
  • 2,612
  • 3
  • 22
  • 37
  • What type is `ProgressBarUpload.Value`? – Yuval Itzchakov May 23 '15 at 20:07
  • I'm not sure it's a good idea to use a `using` statement here since `wc` will be disposed when leaving the scope and the event handler will hang on to a disposed object. Try the same code but without the `using` block. – Christian May 23 '15 at 20:15
  • @YuvalItzchakov , ProgressBarUpload.Value is Double, e.ProgressPercentage is Int. But I do not think that the problem in this.e.ProgressPercentage returns incorrect value – Mikhail May 24 '15 at 05:30
  • @Christian , It does not work.And FileUploadProgressChanged work true. I wrote that after the 50% returns wrong value, and then immediately to 100%. – Mikhail May 24 '15 at 05:35

1 Answers1

1

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.

Community
  • 1
  • 1
Mikhail
  • 2,612
  • 3
  • 22
  • 37