【发布时间】:2011-04-20 16:57:00
【问题描述】:
当我尝试在我的网站上上传视频时,我收到了错误超出了最大请求长度。
我该如何解决这个问题?
【问题讨论】:
标签: asp.net iis file-upload
当我尝试在我的网站上上传视频时,我收到了错误超出了最大请求长度。
我该如何解决这个问题?
【问题讨论】:
标签: asp.net iis file-upload
如果您使用 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 以 千字节 为单位
maxAllowedContentLength 以 字节 为单位 这就是此配置示例中的值不同的原因。 (两者都相当于 1 GB。)
【讨论】:
Web.config 而不是 Views 文件夹中的设置
IMPORTANT: Both of these values must match. In this case, my max upload is 1024 megabytes. 来自 Karls 的回答 以纠正此问题,然后就可以了。
我认为这里没有提到它,但要使其正常工作,我必须在 web.config 中提供这两个值:
在system.web
<httpRuntime maxRequestLength="1048576" executionTimeout="3600" />
在system.webServer
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1073741824" />
</requestFiltering>
</security>
重要提示:这两个值必须匹配。在这种情况下,我的最大上传是 1024 兆字节。
maxRequestLength 有 1048576 KILOBYTES,而 maxAllowedContentLength 有 1073741824 BYTES。
我知道这很明显,但很容易被忽视。
【讨论】:
web.config 文件中。
值得注意的是,您可能希望将此更改限制为您希望用于上传的 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>
【讨论】:
并且以防万一有人正在寻找一种方法来处理此异常并向用户显示有意义的解释(例如“您上传的文件太大”):
//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 属性。
maxAllowedContentLength,这似乎不会被抛出。在这种情况下,IIS 似乎会在调用 ASP 之前做出响应。所以需要设置这个非常大
如果您无法更新配置文件但控制处理文件上传的代码,请使用HttpContext.Current.Request.GetBufferlessInputStream(true)。
disableMaxRequestLength 参数的 true 值告诉框架忽略配置的请求限制。
详细说明请访问https://msdn.microsoft.com/en-us/library/hh195568(v=vs.110).aspx
【讨论】:
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 为单位) 这里作为 ex。我取了 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文件, 我应该修改 ROOT 目录中的那个,而不是其他的。 希望这会有所帮助。
【讨论】:
如果您有请求访问站点中的应用程序,请确保在根 web.config 中设置 maxRequestLength。应用程序的 web.config 中的 maxRequestLength 似乎被忽略了。
【讨论】:
httpRuntime maxRequestLength="### 和 requestLimits maxAllowedContentLength 放在根级别的 Web 配置中,而不是子应用级别。
我们的 web.config 文件有多个 system.web 部分,这让我大吃一惊:当我在配置级别将 添加到 system.web 部分时,它起作用了。
【讨论】:
我必须编辑C:\Windows\System32\inetsrv\config\applicationHost.config 文件并将<requestLimits maxAllowedContentLength="1073741824" /> 添加到...的末尾...
<configuration>
<system.webServer>
<security>
<requestFiltering>
部分。
【讨论】:
applicationHost.config。
我正在处理同样的错误,并在花费时间后通过在 web.config 文件中添加以下行来解决它
<system.web>
<httpRuntime targetFramework="4.7.1" maxRequestLength="1048576"/>
</system.web>
和
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1073741824" />
</requestFiltering>
</security>
</system.webServer>
【讨论】:
我可以添加到未编译的配置 web
<system.web>
<httpRuntime maxRequestLength="1024" executionTimeout="3600" />
<compilation debug="true"/>
</system.web>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1048576"/>
</requestFiltering>
</security>
【讨论】:
executionTimeout 属性与询问的内容无关,compilation 标记也没有。