在我尝试上传视频到我的网站时,出现了Maximum request length exceeded的错误提示。
我该如何解决这个问题?
在我尝试上传视频到我的网站时,出现了Maximum request length exceeded的错误提示。
我该如何解决这个问题?
如果您使用IIS托管应用程序,则默认的上传文件大小为4MB。要增加它,请在web.config
中使用下面的代码段 -
<configuration>
<system.web>
<httpRuntime maxRequestLength="1048576" />
</system.web>
</configuration>
对于 IIS7 及以上版本,您还需要添加以下行:
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1073741824" />
</requestFiltering>
</security>
</system.webServer>
注意:
maxRequestLength
的单位为千字节(KB)maxAllowedContentLength
的单位为字节(B)因此在这个配置示例中,两个值的差异是由于单位不同。(它们都等于1GB。)
我认为这里没有提到,但要使其起作用,我必须在web.config中提供这两个值:
在system.web
中
<httpRuntime maxRequestLength="1048576" executionTimeout="3600" />
在 system.webServer
中
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1073741824" />
</requestFiltering>
</security>
重要提示:这两个值必须相匹配。在这种情况下,我的最大上传大小为1024兆字节。
maxRequestLength为1048576千字节,maxAllowedContentLength为1073741824字节。
我知道这很明显,但很容易忽略。
web.config
文件中找到。 - Miguel Angelo值得注意的是,您可能希望将此更改限制为您期望用于上传的URL,而不是整个站点。
<location path="Documents/Upload">
<system.web>
<!-- 50MB in kilobytes, default is 4096 or 4MB-->
<httpRuntime maxRequestLength="51200" />
</system.web>
<system.webServer>
<security>
<requestFiltering>
<!-- 50MB in bytes, default is 30000000 or approx. 28.6102 Mb-->
<requestLimits maxAllowedContentLength="52428800" />
</requestFiltering>
</security>
</system.webServer>
</location>
如果有人正在寻找处理此异常并向用户显示有意义解释的方法(例如“您正在上传一个太大的文件”),请参考以下内容:
而为了防止这种情况发生,你可以在上传前检查文件大小。
不过如果你已经收到了该错误信息,你可以尝试联系管理员或稍后再试。
Good luck!
//Global.asax
private void Application_Error(object sender, EventArgs e)
{
var ex = Server.GetLastError();
var httpException = ex as HttpException ?? ex.InnerException as HttpException;
if(httpException == null) return;
if (((System.Web.HttpException)httpException.InnerException).WebEventCode == System.Web.Management.WebEventCodes.RuntimeErrorPostTooLarge)
{
//handle the error
Response.Write("Too big a file, dude"); //for example
}
}
(需要 ASP.NET 4 或更高版本)
HttpContext.Current.ClearError()
才能使Response.Redirect()
正常工作。在web.config
方面,它只与httpRuntime
的maxRequestLength
属性一起工作。 - nrodonchange
事件并将上传文件大小与最大上传限制进行比较,在页面级别上使用JS执行文件大小验证和友好提示。 - Alfred WallacemaxAllowedContentLength
,似乎不会抛出此异常。在这种情况下,IIS似乎会在ASP被调用之前进行响应。因此需要将其设置得非常大。 - Garr GodfreyHttpContext.Current.Request.GetBufferlessInputStream(true)
。
disableMaxRequestLength
参数中的true
值告诉框架忽略配置的请求限制。在web.config中有一个元素用于配置上传文件的最大大小:
<httpRuntime
maxRequestLength="1048576"
/>
总结所有答案到一个单独的位置:
<system.web>
<httpRuntime targetFramework="4.5.2" maxRequestLength="1048576"/>
</system.web>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1073741824" />
</requestFiltering>
</security>
</system.webServer>
规则:
注意事项:
更多信息请参见MSDN
maxRequestLength(以KB为单位的长度),这里以1024(1MB)为例。maxAllowedContentLength(以字节为单位的长度)应该与您的maxRequestLength相同(1048576字节=1MB)。
<system.web>
<httpRuntime maxRequestLength="1024" executionTimeout="3600" />
</system.web>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1048576"/>
</requestFiltering>
</security>
</system.webServer>
这个问题困扰我多天了。 我修改了 Web.config 文件但没有起作用。 后来发现我的项目中有两个 Web.config 文件, 我应该修改在根目录下的那一个,而不是其他的。 希望对你有帮助。
Web.config
文件中,而不是位于Views
文件夹内的文件。 - Serj Sagan