| | |
| | | //允许上传大文件 |
| | | services.Configure<IISServerOptions>(options => |
| | | { |
| | | options.MaxRequestBodySize = 1073741824;//此处限制最大1G |
| | | options.MaxRequestBodySize = 10485760;//此处限制最大10M |
| | | }); |
| | | services.Configure<KestrelServerOptions>(options => |
| | | { |
| | | options.Limits.MaxRequestBodySize = 10485760; // 10M |
| | | }); |
| | | //解决文件上传Multipart body length limit 134217728 exceeded. |
| | | services.Configure<FormOptions>(x => |
| | | { |
| | | //// 设置单个表单值的最大长度 |
| | | x.ValueLengthLimit = int.MaxValue; |
| | | x.MultipartBodyLengthLimit = 1073741824; //此处限制最大1G |
| | | // 设置多部分标头的长度限制 |
| | | x.MultipartHeadersLengthLimit = int.MaxValue; |
| | | //设置整个 multipart 表单的最大长度(关键设置) |
| | | x.MultipartBodyLengthLimit = 10485760; //此处限制最大10M |
| | | }); |
| | | #endregion 允许大文件上传 |
| | | } |
| | |
| | | } |
| | | }); |
| | | app.UseStaticFiles(); |
| | | app.Use(async (context, next) => |
| | | { |
| | | context.Request.EnableBuffering(); // 确保请求可以被多次读取 |
| | | await next(); |
| | | }); |
| | | // 设置请求体大小限制 |
| | | app.Use(async (context, next) => |
| | | { |
| | | if (context.Request.ContentLength > 10 * 1024 * 1024) // 10MB |
| | | { |
| | | var message = new ToMessage |
| | | { |
| | | code = "300", |
| | | message = "请求体过大,请检查文件大小。" |
| | | }; |
| | | |
| | | // 设置正确的状态码并返回序列化后的JSON |
| | | context.Response.StatusCode = 300; // 使用 413 Request Entity Too Large |
| | | context.Response.ContentType = "application/json; charset=utf-8"; // 明确内容类型 |
| | | |
| | | // 将对象序列化为JSON字符串 |
| | | string jsonResponse = System.Text.Json.JsonSerializer.Serialize(message); |
| | | await context.Response.WriteAsync(jsonResponse); |
| | | |
| | | return; // 重要:直接返回,不再调用后续中间件 |
| | | } |
| | | await next(); // 如果未超限,则继续执行管道中的下一个中间件 |
| | | }); |
| | | //跨域 |
| | | app.UseCors("cors"); |
| | | //用户session服务 |