<configuration>
<system.web>
<httpRuntime maxRequestLength="1048576" />
</system.web>
</configuration>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1073741824" />
</requestFiltering>
</security>
</system.webServer>
<httpRuntime maxRequestLength="1048576" executionTimeout="3600" />
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1073741824" />
</requestFiltering>
</security>
<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(httpException.WebEventCode == WebEventCodes.RuntimeErrorPostTooLarge)
{
//handle the error
Response.Write("Too big a file, dude"); //for example
}
}
默认情况下,最大请求大小为 4mb(4096 KB)
此处对此进行了说明: https : //support.microsoft.com/zh-cn/help/295626/prb-cannot-upload-large-files-when-you-use-the-htmlinputfile-server-co
上面的文章还介绍了如何解决此问题:)
如果您无法更新配置文件但可以控制处理文件上传的代码,请使用HttpContext.Current.Request.GetBufferlessInputStream(true)
。
disableMaxRequestLength
参数的true
值告诉框架忽略配置的请求限制。
有关详细说明,请访问https://msdn.microsoft.com/zh-cn/library/hh195568(v=vs.110).aspx
<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>
<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目录中的一个,而不是其他文件。希望这会有所帮助。