Open Closed

Blazor webapp 大文件上传 #10379


User avatar
0
mc86 created

你好 https://learn.microsoft.com/zh-cn/aspnet/core/blazor/file-uploads?view=aspnetcore-10.0#file-size-read-and-upload-limits blazor wasm有2G的文件上传大小限制 如何实现大文件上传呢?


16 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    你好

    你没有Chrome浏览器环境?

    如果是非Chrome浏览器, 这个问题无法解决, 属于浏览器的限制.

    谢谢

  • User Avatar
    0
    mc86 created

    有Chrome浏览器环境.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Chrome浏览器应该没有限制, 你现在得到了什么错误和异常信息?

  • User Avatar
    0
    mc86 created

    abp suite的file 属性,直接将文件 copy到少stream. 大文件的话js arry会报内存溢出

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    你可以分享一些代码吗? 组件的和copy stream的

  • User Avatar
    0
    mc86 created
       private async Task<AppFileDescriptorDto> UploadFileAsync(IBrowserFile file)
       {
           using (var ms = new MemoryStream())
           {
               await file.OpenReadStream(long.MaxValue).CopyToAsync(ms);
               ms.Seek(0, SeekOrigin.Begin);
               
               return await ContractAttachmentsAppService.UploadFileAsync(new RemoteStreamContent(ms, file.Name, file.ContentType));
           }
       }
       600MB左右的文件会报 内存溢出
    
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    好的, 我反馈给suite团队, 你可以使用文件流试试

    private async Task<AppFileDescriptorDto> UploadFileAsync(IBrowserFile file)
    {
        // 直接使用文件流,不加载到 MemoryStream
        using var stream = file.OpenReadStream(long.MaxValue);
        
        return await ContractAttachmentsAppService.UploadFileAsync(
            new RemoteStreamContent(stream, file.Name, file.ContentType));
    }
    
  • User Avatar
    0
    mc86 created

    info: System.Net.Http.HttpClient.Contract.LogicalHandler[100] Start processing HTTP request POST https://localhost:44333/api/contract/contract-attachments/upload-file?* invoke-js.ts:242 info: System.Net.Http.HttpClient.Contract.ClientHandler[100] Sending HTTP request POST https://localhost:44333/api/contract/contract-attachments/upload-file?* HTGL:1 The resource https://localhost:44333/_content/Excubo.Blazor.TreeViews/Excubo.Blazor.TreeViews.ggbo2a6x37.bundle.scp.css was preloaded using link preload but not used within a few seconds from the window's load event. Please make sure it has an appropriate as value and it is preloaded intentionally. HTGL:1 The resource [Violation] Forced reflow while executing JavaScript took 116ms

    blazor.web.js:1 Cannot enlarge memory, requested 2586399504 bytes, but the limit is 2147483648 bytes! 报错了.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    你可以分享一个demo项目吗?

    liming.ma@volosoft.com

    谢谢

  • User Avatar
    0
    mc86 created

    已发送

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    好的, 我会检查你的项目

  • User Avatar
    0
    mc86 created

    启用浏览器流上传可以解决~

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    是的, 你可以这样试试

    private async Task<AppFileDescriptorDto> UploadFileAsync(IBrowserFile file)
    {
        // using (var ms = file.OpenReadStream(2L*1024*1024*1024))
        // {
        //     return await UpFilesAppService.UploadFileAsync(new RemoteStreamContent(ms, file.Name, file.ContentType));
        // }
    
        await using (var stream = file.OpenReadStream(MaxUpFileFsFileUploadSize))
        {
            using (var content = new MultipartFormDataContent())
            {
                using (var fileContent = new StreamContent(stream))
                {
                    fileContent.Headers.ContentType = new MediaTypeHeaderValue(file.ContentType ?? "application/octet-stream");
                    content.Add(fileContent, "input", file.Name);
    
                    using var request = new HttpRequestMessage(HttpMethod.Post, "https://localhost:44362/api/app/up-files/upload-file");
                    var token = await AccessTokenProvider.GetTokenAsync();
                    if (!token.IsNullOrWhiteSpace())
                    {
                        request.SetBearerToken(token);
                    }
                    request.Content = content;
                    request.SetBrowserRequestStreamingEnabled(true);
    
                    using var response = await new HttpClient().SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
                    response.EnsureSuccessStatusCode();
    
                    var dto = await response.Content.ReadFromJsonAsync<AppFileDescriptorDto>();
                    return dto!;
                }
            }
        }
    }
    
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    You also need to configure the backend limit.

    Log.Information("Starting Uploadtest.HttpApi.Host.");
    var builder = WebApplication.CreateBuilder(args);
    builder.WebHost.ConfigureKestrel(o =>
    {
        o.Limits.MaxRequestBodySize = 2L*1024 * 1024 * 1024;
    });
    
    builder.Services.Configure<FormOptions>(o =>
    {
        o.MultipartBodyLengthLimit = 2L*1024 * 1024 * 1024;
    });
    
    
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    你还可以继续使用应用服务而不是手动创建HttpClient

    public override void PreConfigureServices(ServiceConfigurationContext context)
    {
        PreConfigure<AbpHttpClientBuilderOptions>(options =>
        {
            options.ProxyClientBuildActions.Add((_, builder) =>
            {
                builder.AddHttpMessageHandler<BrowserRequestStreamingHandler>();
            });
        });
    }
    
    public sealed class BrowserRequestStreamingHandler : DelegatingHandler, ITransientDependency
    {
        protected override async Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage request,
            CancellationToken cancellationToken)
        {
            request.SetBrowserRequestStreamingEnabled(true);
            return await base.SendAsync(request, cancellationToken);
        }
    }
    
  • User Avatar
    0
    mc86 created

    已经添加了 谢谢

Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.2.0-preview. Updated on February 17, 2026, 09:10
1
ABP Assistant
🔐 You need to be logged in to use the chatbot. Please log in first.