Request.Form抛出异常

编程入门 行业动态 更新时间:2024-10-10 19:23:28
本文介绍了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抛出异常

本文发布于:2023-06-06 09:48:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/539054.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:抛出   异常   Request   Form

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!