Request.Form抛出异常 [英] Request.Form throws exception
问题描述
我正在制作ASP.NET Core Web应用程序,我正在从javascript文件通过HttpContext上传PDF文件。因此,当我尝试在服务器端加载文件时,使用Request.Form.File,Form抛出System.IO.InvalidDataException类型的异常。表格信息是:超出部分体长限制16384。我尝试编辑web.config文件以增加该限制,但消息始终相同。
我有什么遗漏或者我看错了吗?
I am making ASP.NET Core web application, and I am uploading PDF file through HttpContext from javascript file. So, when I am trying to load file on the server side, using Request.Form.File, Form is throwing exception of type System.IO.InvalidDataException. Form message is saying: "Multipart body length limit 16384 exceeded". I tried to edit web.config file in order to increase that limit, but message is always the same. Is there anything I am missing or I am looking on the wrong side?
谢谢。
推荐答案
定义此属性:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class RequestFormSizeLimitAttribute : Attribute, IAuthorizationFilter, IOrderedFilter
{
private readonly FormOptions _formOptions;
public RequestFormSizeLimitAttribute(int valueCountLimit)
{
_formOptions = new FormOptions()
{
ValueCountLimit = valueCountLimit
};
}
public int Order { get; set; }
public void OnAuthorization(AuthorizationFilterContext context)
{
var features = context.HttpContext.Features;
var formFeature = features.Get<IFormFeature>();
if (formFeature == null || formFeature.Form == null)
{
// Request form has not been read yet, so set the limits
features.Set<IFormFeature>(new FormFeature(context.HttpContext.Request, _formOptions));
}
}
}
并将此属性添加到您的动作方法看看会发生什么:
And add this attribute to your action method see what happens:
[RequestFormSizeLimit(valueCountLimit: 2147483648)]
[HttpPost]
public IActionResult ActionMethod(...)
{
...
}
这篇关于Request.Form抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!