Kestrel MaxRequestBodySize 上传文件超过限制
Kestrel MaxRequestBodySize uploading file over the limit
我确实遇到了 kestrel 的奇怪问题。我无法上传超过 kestrel 的 MaxRequestBodySize 的多个文件。
预期的行为是在我尝试 reader 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]
Connection id "0HLDB9K94VV9M" bad request data: "Request body too large."
Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException:
Request body too large. at
Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Frame.ThrowRequestRejected(RequestRejectionReason
reason) at
Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.MessageBody.ForContentLength.OnReadStart()
at
Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.MessageBody.TryInit()
at
Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.MessageBody.d__24.MoveNext()
--- End of stack trace from previous location where exception was thrown --- at
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task) at
Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Frame`1.d__2.MoveNext()
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 7618.6319ms 413
已编辑
我知道我可以禁用该限制,但在这种情况下是不可能的。
您必须配置两件事:
在你的Program.cs
public static IWebHost BuildWebhost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseKestrel(options => {
options.Limits.MaxRequestBodySize = null; // or a given limit
})
.Build();
在你的Startup.cs ConfigureService 方法中
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 并尝试上传大文件。
根据 this announcement,可以在给定的 [RequestSizeLimit(100000000)]
(或 [DisableRequestSizeLimit]
禁用限制)上针对所有操作全局覆盖最大限制,或者专门针对单个操作动作。
我确实遇到了 kestrel 的奇怪问题。我无法上传超过 kestrel 的 MaxRequestBodySize 的多个文件。
预期的行为是在我尝试 reader 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] Connection id "0HLDB9K94VV9M" bad request data: "Request body too large." Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException: Request body too large. at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Frame.ThrowRequestRejected(RequestRejectionReason reason) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.MessageBody.ForContentLength.OnReadStart() at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.MessageBody.TryInit() at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.MessageBody.d__24.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Frame`1.d__2.MoveNext() info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 7618.6319ms 413
已编辑 我知道我可以禁用该限制,但在这种情况下是不可能的。
您必须配置两件事:
在你的Program.cs
public static IWebHost BuildWebhost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseKestrel(options => {
options.Limits.MaxRequestBodySize = null; // or a given limit
})
.Build();
在你的Startup.cs ConfigureService 方法中
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 并尝试上传大文件。
根据 this announcement,可以在给定的 [RequestSizeLimit(100000000)]
(或 [DisableRequestSizeLimit]
禁用限制)上针对所有操作全局覆盖最大限制,或者专门针对单个操作动作。