取消

Asp.net core上传文件大小限制

如果你正在使用asp.net core接收上传文件,如果上传的文件超过了30mb,那么多半会遇到上传不了的问题。


你需要按以下几种方式添加配置

对于在 IIS 上运行的应用程序(含IIS Express)

在web.config中添加如下配置

1
2
3
4
5
6
7
8
<system.webServer>
  <security>
    <requestFiltering>
      <!-- Handle requests up to 1 GB -->
      <requestLimits maxAllowedContentLength="1073741824" />
    </requestFiltering>
  </security>
</system.webServer>

在 Kestrel 上运行的应用程序

在Startup文件的ConfigureServices方法中添加

1
2
3
4
services.Configure<KestrelServerOptions>(options =>
{
     options.Limits.MaxRequestBodySize = int.MaxValue; // if don't set default value is: 30 MB
});

如果是使用IFormFile参数接收文件以上配置就够了

使用Request.Form.Files接收文件配置(未验证)

1
2
3
4
5
6
services.Configure<FormOptions>(x =>
{
     x.ValueLengthLimit = int.MaxValue;
     x.MultipartBodyLengthLimit = int.MaxValue; // if don't set default value is: 128 MB
     x.MultipartHeadersLengthLimit = int.MaxValue;
});

参考资料

本文会经常更新,请阅读原文: https://dashenxian.github.io/post/Asp.net-core%E4%B8%8A%E4%BC%A0%E6%96%87%E4%BB%B6%E5%A4%A7%E5%B0%8F%E9%99%90%E5%88%B6 ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

知识共享许可协议

本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名 小神仙 (包含链接: https://dashenxian.github.io ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系 (125880321@qq.com)

登录 GitHub 账号进行评论