Upload progress using HttpClient in Blazor Wasm

扶醉桌前 提交于 2020-01-06 08:16:51

问题


I'm trying to get file upload progress inside a blazor web assembly app.

var handler = new ProgressMessageHandler();
handler.InnerHandler = new WebAssemblyHttpMessageHandler();
handler.HttpSendProgress += (object sender, HttpProgressEventArgs e) =>
{
    Console.WriteLine("bytes transfered= " + e.BytesTransferred.ToString());
    Console.WriteLine("total bytes= " + e.TotalBytes.ToString());
    Console.WriteLine("progress percentage= " + e.ProgressPercentage.ToString());
};

var http = new HttpClient(handler);
http.BaseAddress = new Uri(NavigationManager.BaseUri);
await http.PostJsonAsync("api/...", attachedFile);

Console.WriteLine("Done");

I came up with the following code, but when I upload a file I get a single event instantly reporting 100% completion even if the post request last a few seconds.

WASM: bytes transfered= 277983
blazor.webassembly.js:1 WASM: total bytes= 277983
blazor.webassembly.js:1 WASM: progress percentage= 100

There is a few questions on SO dealing with upload progress but I want to use the Http.ProgressMessageHandler if possible

来源:https://stackoverflow.com/questions/58095977/upload-progress-using-httpclient-in-blazor-wasm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!