当前位置:网站首页>Post to asp Net core length limitation and solution when transferring data

Post to asp Net core length limitation and solution when transferring data

2022-06-22 17:03:00 Dotnet cross platform

Use HTTP The standard practice for uploading files under the agreement is : Use  multipart/form-data . But sometimes the files to be uploaded are not too large for simplicity , Will still apply  application/x-www-form-urlencoded  Upload files , This requires encoding binary files before uploading , For example, use Base64 .

public async Task<IActionResult> SubmitImage(int id, [FromForm(Name = "image")] string image)
{
    if (string.IsNullOrWhiteSpace(image)) throw new UserFriendlyException(" Picture cannot be empty ");
    var data = Convert.FromBase64String(image);
    //TODO: Processing images 
}

The above code has been working well , After a long time, I received feedback from customers that several pictures failed to be uploaded , Clearly selected the picture but still prompted “ Picture cannot be empty ”. This explanation image Parameters are not properly bound . The size of the picture is 6MB about , There is obviously no breakthrough IIS The limitation of ( The default is 30MB), There was no breakthrough ASP.NET Ccor Limits on the size of a single request ( The default is 30MB). I went specially to check Windows journal , No error message is seen .

Simulate a user's request , It is HTTP I saw the picture in the packet capture , But in the debugger image It's really empty . After several searches , I found one named  FormOptions  Configuration class :

https://docs.microsoft.com/zh-cn/dotnet/api/microsoft.aspnetcore.http.features.formoptions?view=aspnetcore-6.0

Among them  ValueLengthLimit  Attributes are defined as follows :

A limit on the length of individual form values. Forms containing values that exceed this limit will throw an InvalidDataException when parsed. Defaults to 4,194,304 bytes‬, which is approximately 4MB.

in other words : By default ,ASP.NET Core Limits every one of them POST The length of the data value is 4 MB size , If it exceeds, it will be thrown  InvalidDataException  abnormal . I probably encountered this limitation , But I'm not sure because I didn't throw an exception .

services.Configure<FormOptions>(options=>{
  options.ValueLengthLimit = 209715200;//200MB
});

The above code raises the limit to 200MB, The tested problem is solved .

Use Base64 Although it will be simplified after encoding the file , But at the same time, the network transmission content will increase , Or encounter the exception shown in this article ( By default ,multipart/form-data  The limit on a single upload item is 128MB). If you're using MVC, It can also be used directly  RequestFormLimitsAttribute  Solve the problem :

https://docs.microsoft.com/zh-cn/dotnet/api/microsoft.aspnetcore.mvc.requestformlimitsattribute?view=aspnetcore-6.0

原网站

版权声明
本文为[Dotnet cross platform]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221536307395.html