Ass*_*sin 1 .net kestrel-http-server asp.net-core
我的茶k确实遇到了一个奇怪的问题。我无法上传超过茶est的MaxRequestBodySize的多个文件。
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.
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
};
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
asp.net core logs:
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();
Run Code Online (Sandbox Code Playgroud)
在您的Startup.cs中的ConfigureService方法
services.Configure<FormOptions>(options => options.MultipartBodyLengthLimit = long.MaxValue); // or other given limit
Run Code Online (Sandbox Code Playgroud)
同时更改您的控制器端点以使用 [FromForm]
public IActionResult Upload([FromForm] IEnumerable<IFormFile> files)... // name must be same as name attribute of your multipart form
Run Code Online (Sandbox Code Playgroud)
现在,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
Run Code Online (Sandbox Code Playgroud)
然后导航到http:// localhost:5000 / index.html并尝试上传大文件。
| 归档时间: |
|
| 查看次数: |
1995 次 |
| 最近记录: |