当前位置:网站首页>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
边栏推荐
- [MYSQL]一台windows电脑安装多个mysql-不同版本
- [pop up box at the bottom of wechat applet package] I
- WPF效果第一百九十篇之再耍ListBox
- Uniapp wechat applet obtains page QR code (with parameters)
- [wechat applet custom bottom tabbar]
- spark的NaiveBayes中文文本分类
- 你管这破玩意儿叫高可用?
- In case of default import failure
- [C language] use of library function qsort
- Spark性能调优之道——解决Spark数据倾斜(Data Skew)的N种姿势
猜你喜欢

web技术分享| 【高德地图】实现自定义的轨迹回放

Short video source code development, high-quality short video source code need to do what?

.NET 发布和支持计划介绍

In case of default import failure

0基础如何入门软件测试,能转行成功吗?

ABP Framework 5.3.0 版本新增功能和变更说明

In the era of video explosion, who is supporting the high-speed operation of video ecological network?

Make the code elegant (learn debugging + code style)

JMeter use case

NLog自定义Target之MQTT
随机推荐
快速掌握 ASP.NET 身份认证框架 Identity - 用户注册
Scala equality
mysql 字符串字段转浮点型字段
同花顺是什么?在线开户安全么?
Hydra installation and use
图计算Hama-BSP模型的运行流程
你管这破玩意儿叫高可用?
Iterators and generators
基于.NetCore开发博客项目 StarBlog - (12) Razor页面动态编译
Qt筆記-QMap自定義鍵(key)
spark-shuffle的写入器源码分析
Scala for derivation: the ability to define a value in the first part of a for expression and use it in subsequent (outer) expressions
Qt笔记-QMap自定义键(key)
jsp学习之开发环境的配置
Spark streaming receiver startup and data receiving
同花顺容易开户么?网上开户安全么?
Vhedt business development framework
团队管理|如何提高技术 Leader 的思考技巧?
What is restful and what rules should be followed when designing rest APIs?
C#-Linq源码解析之DefaultIfEmpty