我确实遇到了红隼的一个奇怪的问题。我无法上传超过 kestrel MaxRequestBodySize 的多个文件。
预期的行为是抛出BadHttpRequestException当我尝试阅读时this.Request.Form.Files.GetFiles()。我确实希望仅收到一次对控制器操作的请求。
发生的情况是上传操作被点击了几次,并且浏览器显示消息“连接丢失”。我没有找到有关调用该操作的次数的模式。
控制器动作:
[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
};
}
}
view:
<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核心日志:
信息: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()
--- 从先前抛出异常的位置开始的堆栈跟踪结束 --- at
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
Edited我知道我可以禁用该限制,但在这种情况下这是不可能的。
您必须配置两件事:
在你的 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 将完成这项工作并按顺序从表单注入文件。
Edit:
我创建了一个可以从 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 http://localhost:5000/index.html并尝试上传大文件。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)