【问题标题】:How to increase the limit on the size of the uploaded file?如何增加上传文件的大小限制?
【发布时间】:2018-12-18 09:01:42
【问题描述】:

我使用ABP Zero Core MVC 模板(ASP.Net Core 2.x、MVC、jQuery)4.2.0 版。当我尝试使用 AJAX 将文件上传到控制器时,我收到 HTTP 404.13 错误,这表明已超出最大文件大小。 Herehere 我找到了类似问题的解决方案并尝试将其解决得如此恰当,但它们要么不符合使用的模式,要么我做错了什么。

零核如何增加最大上传文件大小?

// *.Web.Host\Startup\Program.cs 
// *.Web.Mvc\Startup\Program.cs
public static IWebHostBuilder CreateWebHostBuilder(string[] args) {
    return WebHost.CreateDefaultBuilder(args)
        .UseKestrel(options => { 
            // no effect...  I can only small files uploaded
            options.Limits.MaxRequestBodySize = 1024 * 1024 * 1024; 
        })
        .UseStartup<Startup>();
}

【问题讨论】:

  • 到目前为止,您还尝试了哪些其他方法?您可能需要多种技术组合,因为这不是一个位置的限制。
  • @Kami 我尝试在 * .Web.Mvc 和 * .Web.Host 项目的 Program.cs 文件中使用上面指定的代码。
  • 你试过github.com/aspnet/Announcements/issues/267的设置了吗?
  • @Kami 全局配置变体 - 无效,RequestSizeLimit 属性变体 - 无效,中间件变体 - 我没有找到任何文件。
  • 你是独立做这些的还是同时做的?此外,除了这些设置之外,您可能还需要更改 IIS 配置。

标签: c# asp.net-core asp.net-core-mvc aspnetboilerplate


【解决方案1】:

您是否尝试使用[RequestSizeLimit(YOUR_MAX_TOTAL_UPLOAD_SIZE)] 来装饰控制器action 以及Startup.cs 中的更改?

services.Configure<FormOptions>(opt =>
{
    opt.MultipartBodyLengthLimit = YOUR_MAX_TOTAL_UPLOAD_SIZE;
});

顺便说一句,如果您打算在 IIS 上托管应用程序,您可以将 web.config 文件添加到您的项目并在那里配置 upload size,而不是在 IIS 服务器上配置它。

编辑:作为@XelaNimed 的评论,添加web.config 文件以及编辑startup.cs,使代码正常工作。

<configuration>
  <location path="." inheritInChildApplications="false">
   <system.webServer>
     <handlers>
       <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
     </handlers>
     <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout">
       <environmentVariables />
     </aspNetCore>
   </system.webServer>
 </location>
 <system.webServer>
   <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="YOUR_BYTES" />
       </requestFiltering>
    </security>
  </system.webServer>
</configuration>

【讨论】:

  • 我尝试用RequestSizeLimit 属性装饰方法,但错误Multipart body length limit 134217728 exceeded 仍然出现。编辑*.Web.Mvc/Startup/Startup.cs 文件和编辑*.Web.Mvc/web.config 的解决方案有效。请编辑您的代码,如上面的答案,因为不清楚哪些文件和方法需要编辑。
【解决方案2】:

试试这个

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
  //add to beginning of the method... 
  services.Configure<FormOptions>(x =>
  {
      x.ValueLengthLimit = int.MaxValue;
      x.MultipartBodyLengthLimit = int.MaxValue;
      x.MultipartHeadersLengthLimit = int.MaxValue;
  });


}

【讨论】:

  • 感谢您的回复,但出现同样的错误:HTTP 404.13
猜你喜欢
  • 2012-04-26
  • 2019-02-08
  • 2010-09-27
  • 1970-01-01
  • 2015-05-07
  • 2019-02-27
  • 2017-08-20
  • 2023-01-28
  • 1970-01-01
相关资源
最近更新 更多