当前位置:   article > 正文

netcore中,使用IFormFile从Form中接收大文件报错

iformfile

       点击查看详细报错信息:

Request body too large. The max request body size is 30000000 bytes.

意思是超出了body的最大限制30M。

     net出于安全考虑,对body的大小做了限制,超出了限制就会form转化失败,导致我们Reuest.Form为空,最终抛出异常,解决办法也很简单,主要从以下三个方面对症下药:

1. IIS content length limit

The default request limit (maxAllowedContentLength) is 30,000,000 bytes, which is approximately 28.6MB. Customize the limit in the web.config file:

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

Note: Without this application running on IIS would not work.

2. ASP.NET Core Request length limit:

For application running on IIS:

  1. services.Configure<IISServerOptions>(options =>
  2. {
  3. options.MaxRequestBodySize = int.MaxValue;
  4. });

For application running on Kestrel:

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

3. Form's MultipartBodyLengthLimit

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

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/377884
推荐阅读
相关标签
  

闽ICP备14008679号