gpt4 book ai didi

.net - Kestrel MaxRequestBodySize 上传文件超限

转载 作者:行者123 更新时间:2023-12-01 15:02:48 27 4
gpt4 key购买 nike

我确实遇到了红隼的一个奇怪问题。我无法上传超过红隼 MaxRequestBodySize 的多个文件。
当我试图阅读 this.Request.Form.Files.GetFiles() 时,预期的行为是抛出 BadHttpRequestException。我确实希望只收到一次对 Controller 操作的请求。
发生的事情是上传操作被点击了几次,浏览器显示消息“连接丢失”。我没有找到关于 Action 被调用的时间的模式。
Controller Action :

[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. atMicrosoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Frame.ThrowRequestRejected(RequestRejectionReasonreason) atMicrosoft.AspNetCore.Server.Kestrel.Core.Internal.Http.MessageBody.ForContentLength.OnReadStart()atMicrosoft.AspNetCore.Server.Kestrel.Core.Internal.Http.MessageBody.TryInit()atMicrosoft.AspNetCore.Server.Kestrel.Core.Internal.Http.MessageBody.d__24.MoveNext()--- End of stack trace from previous location where exception was thrown --- atSystem.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() atSystem.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Tasktask) atMicrosoft.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();

在 ConfigureService 方法中的 Startup.cs
services.Configure<FormOptions>(options => options.MultipartBodyLengthLimit = long.MaxValue); // or other given limit

还要更改您的 Controller 端点以使用 [FromForm]
public IActionResult Upload([FromForm] IEnumerable<IFormFile> files)... // name must be same as name attribute of your multipart form

现在 ASP.NET Core 将完成这项工作并将表单中的文件作为序列注入(inject)。

编辑:

我创建了一个可以从 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并尝试上传大文件。

关于.net - Kestrel MaxRequestBodySize 上传文件超限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50037706/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com