简体   繁体   English

Blazor UI 在 memory 文件上传期间挂起

[英]Blazor UI hanging during memory file upload

I'm working on a simple blazor application that receives a file upload and stores it.我正在开发一个简单的 blazor 应用程序,它接收文件上传并存储它。 I am using BlazorInputFile and I can't work out why copying the stream to MemoryStream is causing the browser to freeze.我正在使用BlazorInputFile ,但我无法弄清楚为什么将 stream 复制到 MemoryStream 会导致浏览器冻结。

The details of how to use (and how it's implemented) BlazorInputFile are explained in this blog post: Uploading Files in Blazor .此博客文章中解释了如何使用(以及如何实现) BlazorInputFile的详细信息: 在 Blazor 中上传文件

 var ms = new MemoryStream();
 await file.Data.CopyToAsync(ms); // With a 1MB file, this line took 3 seconds, and froze the browser

 status = $"Finished loading {file.Size} bytes from {file.Name}";

Sample project/repo: https://github.com/paulallington/BlazorInputFileIssue (this is just the default Blazor app, with BlazorInputFile implemented as per the article)示例项目/repo:https://github.com/paulallington/BlazorInputFileIssue (这只是默认的 Blazor 应用程序,BlazorInputFile 按照文章实现)

I've experienced the same issue.我遇到过同样的问题。 I've tried both predefined components such as Steve Sanderssons file upload and MatBlazor fileupload and also my own component to handle fileuploads.我已经尝试了两个预定义的组件,例如 Steve Sanderssons 文件上传和 MatBlazor 文件上传,以及我自己的处理文件上传的组件。 Small files are not a problem.小文件不是问题。 Once the files are a bit larger in size the UI will hang itself.一旦文件变大一点,UI 就会自行挂起。 MemoryOutOfBoundsException (or similar). MemoryOutOfBoundsException(或类似的)。 So no, async/await can't help you release the UI.所以不,async/await 不能帮助你释放 UI。 I have put so much effort into this issue, one solution, that I am currently using, is to do all fileuploads with javascript instead of blazor.我在这个问题上付出了很多努力,我目前使用的一种解决方案是使用 javascript 而不是 blazor 进行所有文件上传。 Just use javascript to get the file and post it up to the server.只需使用 javascript 获取文件并将其发布到服务器。 No JSInterop.. However, it seems like it is a memory issue in webassembly mono.没有 JSInterop .. 但是,它似乎是 webassembly mono 中的 memory 问题。 Read more here: https://github.com/dotnet/aspnetcore/issues/15777在此处阅读更多信息: https://github.com/dotnet/aspnetcore/issues/15777

Note: I haven't tried this on the latest Blazor version.注意:我还没有在最新的 Blazor 版本上尝试过这个。 So I'm not sure it's fixed or not.所以我不确定它是否已修复。

Use await Task.Delay(1);使用await Task.Delay(1); as mentioned on Zhi Lv's comment in this post blazor-webassembly upload file can't show progress?正如吕志在这篇文章中的评论中提到的那样 blazor-webassembly 上传文件无法显示进度?

var buffer = new byte[imageFile.Size];
await Task.Delay(1);

await imageFile.OpenReadStream(Int64.MaxValue).ReadAsync(buffer);

pratica.Files.Add(new FilePraticaRequest()
{
    Contenuto = buffer,
    Nome = imageFile.Name,

});
StateHasChanged();

You wait for the copy result, so the app freeze, you can refactor your code like this;您等待复制结果,因此应用程序冻结,您可以像这样重构代码;

var ms = new MemoryStream();
file.Data.CopyToAsync(ms).ContinueWith(async task => 
{
   if (task.Exception != null)
   {
      throw task.Exception; // Update this by your convenience
   }
   status = $"Finished loading {file.Size} bytes from {file.Name}";
   await InvokeAsync(StateHasChanged).ConfigureAwait(false); // informs the component the status changed
}; // With a 1MB file, this line took 3 seconds, and should not froze the browser

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM