kestler MaxRequestBodySize超过限制上传文件

时间:2018-04-26 07:56:50

标签: .net asp.net-core kestrel-http-server

我确实遇到了红隼的一个奇怪的问题。我无法上传超过红隼MaxRequestBodySize的多个文件。

当我尝试读取 this.Request.Form.Files.GetFiles()时,预期的行为是抛出 BadHttpRequestException 。我确实希望只收到一次对控制器动作的请求。

正在发生的事情是上传操作被点击几次,浏览器显示消息“连接丢失”。我没有找到关于这个动作被称为妈妈时间的模式。

控制器操作:

[HttpPost("upload")]
public IActionResult Upload()
{
    try
    {
        var files = this.Request.Form.Files.GetFiles("files");
        files.Select(async file => await this.SaveFile(file))
        return this.RedirectToAction(nameof(VueController.FilesList),"Vue");
    }
    catch (BadHttpRequestException exp)
    {
        return new string[]
        {
            exp.Message
        };
    }
}

视图:

<form method="post"
          enctype="multipart/form-data"
          action="/api/v1/files/upload"
          novalidate="novalidate">
      <input type="file"
             name="files"
             multiple="multiple"
             required="required"
             accept=""
             capture="capture" />
    </form>

asp.net核心日志:

info:Microsoft.AspNetCore.Server.Kestrel [17]       连接ID“0HLDB9K94VV9M”错误请求数据:“请求正文太大”。 Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException:请求正文太大。在Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Frame.ThrowRequestRejected(RequestRejectionReason reason)    在Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.MessageBody.ForContentLength.OnReadStart()    在Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.MessageBody.TryInit()    在Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.MessageBody.d__24.MoveNext() ---从抛出异常的先前位置开始的堆栈跟踪结束---    在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()    在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)    在Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Frame`1.d__2.MoveNext() info:Microsoft.AspNetCore.Hosting.Internal.WebHost [2]       要求在7618.6319ms 413完成

被修改 我知道我可以禁用限制,但在这种情况下是不可能的。

2 个答案:

答案 0 :(得分:2)

您必须配置两件事:

在你的Program.cs中

public static IWebHost BuildWebhost(string[] args) => 
   WebHost.CreateDefaultBuilder(args)
      .UseStartup<Startup>()
      .UseKestrel(options => {
           options.Limits.MaxRequestBodySize = null; // or a given limit
      })
     .Build();

在ConfigureService方法的Startup.cs中

services.Configure<FormOptions>(options => options.MultipartBodyLengthLimit = long.MaxValue); // or other given limit

同时更改控制器端点以使用[FromForm]

public IActionResult Upload([FromForm] IEnumerable<IFormFile> files)... // name must be same as name attribute of your multipart form

现在,ASP.NET Core将完成工作并从表单中按顺序注入文件。

修改

我创建了一个可以从github克隆的示例:

git clone https://github.com/alsami/example-fileupload-aspnet-core.git
cd example-fileupload-aspnet-core
dotnet restore
dotnet run --project src/file-upload-api/file-upload-api.csproj

然后导航到http://localhost:5000/index.html并尝试上传大文件。

答案 1 :(得分:0)

根据this announcement,对于所有操作,可以全局覆盖最大限制,或者对于使用[RequestSizeLimit(100000000)](或[DisableRequestSizeLimit]禁用限制)的单个操作,可以覆盖最大限制。