我正在制作ASP.NET Core Web应用程序,我正在从javascript文件通过HttpContext上传PDF文件。因此,当我尝试在服务器端加载文件时,使用Request.Form.File,Form抛出System.IO.InvalidDataException类型的异常。表格信息是:“超出部分体长限制16384”。我试图编辑web.config文件以增加该限制,但消息始终相同。 我有什么遗漏或者我在找错了吗?
感谢。
答案 0 :(得分:3)
定义此属性:
[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));
}
}
}
并将此属性添加到您的操作方法中,看看会发生什么:
[RequestFormSizeLimit(valueCountLimit: 2147483648)]
[HttpPost]
public IActionResult ActionMethod(...)
{
...
}