Request.Form抛出异常 [英] Request.Form throws exception

查看:1934
本文介绍了Request.Form抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆