Abp如何严格限制文件大小
How to achieve strictly file size limit in Abp
我正在尝试使用过滤器严格设置文件大小限制,与此同时,Startup.cs
[=23= 已经有一个默认的全局设置]
Startup.cs
int sizeLimit = 200000;
services.Configure<IISServerOptions>(options =>
{
options.MaxRequestBodySize = sizeLimit;
});
services.Configure<KestrelServerOptions>(options =>
{
options.Limits.MaxRequestBodySize = sizeLimit;
});
services.Configure<FormOptions>(x => x.MultipartBodyLengthLimit = sizeLimit);
TestApiController.cs 或服务(动态控制器)
[Route("[controller]")]
public class TestApiController : Controller
{
[HttpPost]
[RequestSizeLimit(100_000)]
[RequestFormLimits(MultipartBodyLengthLimit = 100_000)]
public bool Post(IFormFile file)
{
return true;
}
}
我在上传文件时发现警告日志
A request body size limit could not be applied. The IHttpRequestBodySizeFeature for the server is read-only.
我好像需要把IHttpMaxRequestBodySizeFeature
IsReadOnly
设为false,但是不行。 IsReadOnly 中没有 setter。
app.Run(async (context) =>
{
var httpMaxRequestBodySizeFeature = context.Features.Get<IHttpMaxRequestBodySizeFeature>();
httpMaxRequestBodySizeFeature.IsReadOnly = false;
});
然后我发现它会受到IsUpgraded
的影响,如果调用IHttpUpgradeFeature.UpgradeAsync
会将IsUpgraded
设置为true
。我想知道这个方法可能在某个地方调用过,但我想不通。
bool IHttpMaxRequestBodySizeFeature.IsReadOnly => HasStartedConsumingRequestBody || IsUpgraded;
我正在尝试使用过滤器严格设置文件大小限制,与此同时,Startup.cs
[=23= 已经有一个默认的全局设置]
Startup.cs
int sizeLimit = 200000;
services.Configure<IISServerOptions>(options =>
{
options.MaxRequestBodySize = sizeLimit;
});
services.Configure<KestrelServerOptions>(options =>
{
options.Limits.MaxRequestBodySize = sizeLimit;
});
services.Configure<FormOptions>(x => x.MultipartBodyLengthLimit = sizeLimit);
TestApiController.cs 或服务(动态控制器)
[Route("[controller]")]
public class TestApiController : Controller
{
[HttpPost]
[RequestSizeLimit(100_000)]
[RequestFormLimits(MultipartBodyLengthLimit = 100_000)]
public bool Post(IFormFile file)
{
return true;
}
}
我在上传文件时发现警告日志
A request body size limit could not be applied. The IHttpRequestBodySizeFeature for the server is read-only.
我好像需要把IHttpMaxRequestBodySizeFeature
IsReadOnly
设为false,但是不行。 IsReadOnly 中没有 setter。
app.Run(async (context) =>
{
var httpMaxRequestBodySizeFeature = context.Features.Get<IHttpMaxRequestBodySizeFeature>();
httpMaxRequestBodySizeFeature.IsReadOnly = false;
});
然后我发现它会受到IsUpgraded
的影响,如果调用IHttpUpgradeFeature.UpgradeAsync
会将IsUpgraded
设置为true
。我想知道这个方法可能在某个地方调用过,但我想不通。
bool IHttpMaxRequestBodySizeFeature.IsReadOnly => HasStartedConsumingRequestBody || IsUpgraded;