c# - 在Asp.Net Core v3.1中增加上传文件的大小

标签 c# file-upload iis-express asp.net-core-3.1 kestrel-http-server

我正在尝试在.NET Core v3.1 Blazor应用程序中上传多个文件,但无法通过30MB的限制。
通过搜索,我找到了Increase upload file size in Asp.Net core并尝试了建议,但是它不起作用。
找到的所有解决方案都涉及更改web.config,但我没有该文件。
此外,我的应用程序在开发期间在Visual Studio 2019中运行,但也将在Azure上作为WebApp运行。
这些是我的设置:
program.cs

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>().ConfigureKestrel((context, options) =>
            {
                options.Limits.MaxRequestBodySize = null;
            });
        });
UploadController.cs
[Authorize]
[DisableRequestSizeLimit]
public class UploadController : BaseApiController
在Startup.cs中配置服务
services.AddSignalR(e => e.MaximumReceiveMessageSize = 102400000)
    .AddAzureSignalR(Configuration["Azure:SignalR:ConnectionString"]);

services.Configure<FormOptions>(options =>
{
    options.ValueLengthLimit = int.MaxValue;
    options.MultipartBodyLengthLimit = long.MaxValue; // <-- !!! long.MaxValue
    options.MultipartBoundaryLengthLimit = int.MaxValue;
    options.MultipartHeadersCountLimit = int.MaxValue;
    options.MultipartHeadersLengthLimit = int.MaxValue;
});
services.Configure<IISServerOptions>(options =>
{
    options.MaxRequestBodySize = int.MaxValue;
});
在Startup.cs中配置
app.Use(async (context, next) =>
{
    context.Features.Get<IHttpMaxRequestBodySizeFeature>()
        .MaxRequestBodySize = null;

    await next.Invoke();
});
我错过了设置吗?不能相信这需要这么难。

最佳答案

https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-3.1找到了解决方案。最小化的解决方案只是一个新添加的web.config文件,其内容如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="52428800" />
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>
似乎还有其他设置,例如限制每个操作方法。您可能需要检查它们,然后选择最适合您的需求。
p.s.今天早些时候在其他地方看到了相同的web.config解决方案。尝试将30M用作maxAllowedContentLength,但不适用于约10MB的文本文件。现在实现的请求大小增加了三倍,因为文件内容是以二进制数组的字符串表示形式发送的(这是一个问题,应予以处理)。检查网络标签以获取确切的请求大小,并确保它不超过上述的web.config设置。

关于c# - 在Asp.Net Core v3.1中增加上传文件的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62680650/

相关文章:

C# 从 WMV 中提取特定帧

c# - Entity Framework 代码优先 CTP4 默认列值?

c# - 如何在 Microsoft.Extensions.DependencyInjection 中注册现有实例?

amazon-web-services - 用于将多个文件上传到 S3 中的路径的预签名 URL

html - 如何从 Firefox 3 中的 HTML 输入表单获取文件路径

visual-studio-2015 - 为网站项目更改 iis-express Visual Studio 2015 的默认端口

c# - 使用 Fluent API 而不是 InverseProperty 和导航属性

c# - 如何在IIS中下载文件?

visual-studio-2010 - 如何使 IIS Express 成为 Visual Studio 中的默认 Web 服务器?

visual-studio - IIS Express 应该自动安装还是我应该单独安装?