我正在尝试在blazor Web Assembly应用程序中获取文件上传进度。
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");
我想到了以下代码,但是当我上传文件时,即使发布请求持续了几秒钟,我也会收到一个事件,即立即报告100%完成。
WASM: bytes transfered= 277983
blazor.webassembly.js:1 WASM: total bytes= 277983
blazor.webassembly.js:1 WASM: progress percentage= 100
关于SO处理上传进度有一些问题,但我想尽可能使用Http.ProgressMessageHandler