.net Kestrel MaxRequestBodySize上传文件超出限制
我和红隼确实遇到了一个棘手的问题。我无法上传超过红隼MaxRequestBodySize的多个文件 预期的行为是在尝试读取此.Request.Form.Files.GetFiles()时抛出BadHttpRequestException。我确实希望只收到一次控制器操作请求 发生的情况是,上传操作被点击几次,浏览器显示消息“Conconnect lost”。我没有找到一个关于玛米时代的动作叫什么的模式 控制器操作:.net Kestrel MaxRequestBodySize上传文件超出限制,.net,asp.net-core,kestrel-http-server,.net,Asp.net Core,Kestrel Http Server,我和红隼确实遇到了一个棘手的问题。我无法上传超过红隼MaxRequestBodySize的多个文件 预期的行为是在尝试读取此.Request.Form.Files.GetFiles()时抛出BadHttpRequestException。我确实希望只收到一次控制器操作请求 发生的情况是,上传操作被点击几次,浏览器显示消息“Conconnect lost”。我没有找到一个关于玛米时代的动作叫什么的模式 控制器操作: [HttpPost("upload")] public IAc
[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
};
}
}
视图:
asp.net核心日志:
信息:Microsoft.AspNetCore.Server.Kestrel[17]
连接id“0HLDB9K94VV9M”请求数据错误:“请求正文太大。”
Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException:
请求正文太大。在
Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Frame.ThrowRequestRejected(请求拒绝原因
原因)在
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_uu24.MoveNext()
---来自引发异常的上一个位置的堆栈结束跟踪---在
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()位于
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)在
Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Frame`1.d\uuu 2.MoveNext()
信息:Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
请求在7618.6319ms 413中完成
已编辑
我知道我可以禁用限制,但在这种情况下不可能。您必须配置两件事: 在你的程序中
public static IWebHost BuildWebhost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseKestrel(options => {
options.Limits.MaxRequestBodySize = null; // or a given limit
})
.Build();
现在ASP.NET内核将完成这项工作,并按顺序从表单中插入文件
编辑:
我创建了一个可以从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
然后导航到并尝试上载大型文件。根据最大限制,可使用
[RequestSizeLimit(100000000)]
全局覆盖所有操作,或专门覆盖单个操作的最大限制(或[DisableRequestSizeLimit]
禁用限制)在给定操作上。检查此公告^^^您还可以通过[RequestSizeLimit(100000000)]根据链接公告中的说明覆盖每个请求。
您是否也配置了表单选项?如果是,现在有什么错误?请更新OP。我没有在服务中配置选项,让我试试。我更新了答案,请查看并尝试按照建议更改控制器终结点。我已为FromOption添加了configuratiob,但仍然不起作用。行为完全相同。使用可执行示例更新了答案。
services.Configure<FormOptions>(options => options.MultipartBodyLengthLimit = long.MaxValue); // or other given limit
public IActionResult Upload([FromForm] IEnumerable<IFormFile> files)... // name must be same as name attribute of your multipart form
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