Kestrel MaxRequestBodySize上传的文件超出限制 [英] Kestrel MaxRequestBodySize uploading file over the limit
问题描述
我的茶est确实遇到了一个奇怪的问题.我无法上传超过红est的MaxRequestBodySize的多个文件.
I did encounter a wierd problem with kestrel. I am not able to upload the multiple files which exceed the kestrel's MaxRequestBodySize.
当我尝试读取 this.Request.Form.Files.GetFiles()时,预期的行为是抛出 BadHttpRequestException .我确实希望只收到一次对控制器操作的请求.
The expected behaviour is to throw the BadHttpRequestException when I am trying to reader this.Request.Form.Files.GetFiles(). I do expect to recieve request to controller action only once.
发生的事情是,上载动作被击中了几次,并且浏览器显示消息连接丢失".我没有找到关于如何调用该动作的方式的声音.
What is happening is that the upload action is hit a few time and browser with message "conection lost". I did not find a patter on how mamy times the action is called.
控制器动作:
[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核心日志:
asp.net core logs:
信息:Microsoft.AspNetCore.Server.Kestrel [17]连接ID为"0HLDB9K94VV9M".错误的请求数据:请求正文太大".Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException:请求正文过大.在Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Frame.ThrowRequestRejected(RequestRejectionReason原因)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()信息:Microsoft.AspNetCore.Hosting.Internal.WebHost [2]请求在7618.6319ms内完成413
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
已编辑我知道我可以禁用该限制,但是在这种情况下是不可能的.
Edited I am aware that I can disabled the limit but it is not possible in this case.
推荐答案
您必须配置两件事:
在您的Program.cs中
In your 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方法中
In your Startup.cs in the ConfigureService method
services.Configure<FormOptions>(options => options.MultipartBodyLengthLimit = long.MaxValue); // or other given limit
还将您的控制器端点更改为使用 [FromForm]
Also change your controller endpoint to use [FromForm]
public IActionResult Upload([FromForm] IEnumerable<IFormFile> files)... // name must be same as name attribute of your multipart form
现在,ASP.NET Core将完成工作并按顺序从表单中注入文件.
Now ASP.NET Core will do the work and inject the files from the form as sequence.
我创建了一个可以从github复制的示例:
I created an example that can be cloned from 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 并尝试上传大文件.
Then navigate to http://localhost:5000/index.html and try uploading huge files.
这篇关于Kestrel MaxRequestBodySize上传的文件超出限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!