Blazor 服务器文件上传静默崩溃服务器

blazor server file upload silently crashes server

我正在我的 Blazor 服务器应用程序上测试图像上传。为此,我的 .razor 组件如下所示:

@page "/uploadtest"

<h1>Upload Example</h1>
<AuthorizeView>
    <Authorized>
        <p>A simple example with a model, which can upload images</p>
        <label>
            Load Images:
            <InputFile OnChange="@UploadImages" multiple accept=".jpg,.jpeg,.png" />
        </label>

    </Authorized>
    <NotAuthorized>
        <p>To use this application, please <a href="Identity/Account/Login">log in</a> or <a href="Identity/Account/Register">register</a> a new account! </p>
    </NotAuthorized>
</AuthorizeView>

我把代码隐藏在一个单独的 .razor.cs 文件中,class 看起来像这样:

public partial class UploadExample: ComponentBase
{ 
    #region Protected Properties
    [Inject]
    protected AuthenticationStateProvider AuthenticationStateProvider { get; private set; }

    [Inject]
    protected IWebHostEnvironment WebHostEnvironment { get; private set; }
    #endregion

    #region Public Methods
    /// <summary>
    /// File upload
    /// Only images of type .jpg and png are allowed here.
    /// </summary>
    /// <param name="e"></param>
    /// <returns></returns>
    protected async Task UploadImages(InputFileChangeEventArgs ifc)
    {
        var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
        var user = authState.User;

        if (user.Identity.IsAuthenticated)
        {
            try
            {
                string userId = user.FindFirst(c => c.Type.Contains("nameidentifier"))?.Value;
                string currentTime = DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss");
                string path = Path.Combine(WebHostEnvironment.WebRootPath, $@"UserData/{userId}/UploadTest/{currentTime}");
                Directory.CreateDirectory(path);
                var files = ifc.GetMultipleFiles();
                foreach (var file in files)
                {
                    var filePath = Path.Combine(path, file.Name);
                    await using FileStream fs = new(filePath, FileMode.Create);
                    await file.OpenReadStream().CopyToAsync(fs);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
    #endregion
}

这是我的问题:

编辑 在我的浏览器的 Dev-Options 中,出现以下错误:

Error: Connection disconnected with error 'Error: WebSocket closed with status code: 1006 ().'.

一旦我 select 上传任何文件。我测试了不同的东西(with/without 授权,calling/not 调用 UploadImages() 函数,等等...)

我偶然发现了这个问题:Blazor Server inputfile Crashes with certain Chromium Based Browsers
这听起来很像你正在经历的,对吧?

为进一步证明该理论,请下载以下代码:https://github.com/mu88/Whosebug_BlazorServerFileUpload
这基本上是您的代码的简化副本,可在我的 Chrome 和 Firefox 机器上运行。如果你可以 运行 在你的机器上 Chrome 和 Firefox 中的代码,但不能在 Brave 中,我想我们可以确定我们找到了罪魁祸首。