WebClient AsyncUpload progress percentage always returns 50%

前端 未结 1 1825
南方客
南方客 2020-12-19 12:47

i am using Webclient to upload data using Async call to a server,

    WebClient webClient = new WebClient();
   webClient.UploadDataAsync(uri , \"PUT\", buff         


        
相关标签:
1条回答
  • 2020-12-19 12:54

    If you want to monitor the progress of an upload, you'll need to use UploadFileAsync instead of UploadData.

    With UploadDataAsync you are supposed to manually chunk the file and display the progress (at least, that's what I've determined from my own experience in the matter though I haven't seen it written as such on MSDN).

    What you're looking for is to use UploadFileAsync instead, which will call the UploadProgressChanged event correctly. You can then view the event args properties BytesSent and TotalBytesToSend which should be reflected correctly.

    I assume the rationale behind this is that when you're sending data, you can loop over chunks of your data stream and manually increment your progress tracker whereas with a file you cannot (.NET will manage the entire upload for you). Personally, I feel there is something fishy because there is no reason for the UploadProgressChanged event to be called with invalid information in case of UploadDataAsync - either it is called with valid, correct info or it's not called at all.

    At any rate, give UploadFileAsync a shot and see how that goes.

    0 讨论(0)
提交回复
热议问题