超出最大请求长度。

时间:2010-10-04 08:48:29

标签: asp.net iis file-upload

当我尝试在我的网站上传视频时,我收到错误超出最大请求长度

我该如何解决这个问题?

15 个答案:

答案 0 :(得分:1820)

如果您使用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)

答案 1 :(得分:526)

我认为这里没有提到它,但为了实现这一点,我必须在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

我知道这很明显,但很容易忽视。

答案 2 :(得分:183)

值得注意的是,您可能希望将此更改限制为您希望用于上传的网址,而不是整个网站。

<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>

答案 3 :(得分:39)

以防万一有人正在寻找处理此异常的方法并向用户显示有意义的解释(类似于&#34;你正在上传一个太大的文件&#34; ):

//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
    }
}

(需要ASP.NET 4或更高版本)

答案 4 :(得分:26)

默认情况下,最大请求大小为4mb(4096 KB)

这在此解释:http://support.microsoft.com/default.aspx?scid=kb;EN-US;295626

上述文章还解释了如何解决此问题:)

答案 5 :(得分:18)

web.config中有一个元素用于配置上传文件的最大大小:

<httpRuntime 
    maxRequestLength="1048576"
  />

答案 6 :(得分:16)

如果您无法更新配置文件,但使用HttpContext.Current.Request.GetBufferlessInputStream(true)控制处理文件上传的代码。

true参数的disableMaxRequestLength值告诉框架忽略已配置的请求限制。

有关详细说明,请访问https://msdn.microsoft.com/en-us/library/hh195568(v=vs.110).aspx

答案 7 :(得分:8)

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>

答案 8 :(得分:6)

它也困扰了我好几天。 我修改了Web.config文件,但它没有工作。 原来我的项目中有两个Web.config文件, 我应该修改 ROOT 目录中的那个,而不是其他目录。 希望这会有所帮助。

答案 9 :(得分:6)

在一个地方总结所有答案:

<system.web>
  <httpRuntime targetFramework="4.5.2" maxRequestLength="1048576"/>
</system.web>

<system.webServer>
  <security>
    <requestFiltering>
      <requestLimits maxAllowedContentLength="1073741824" />
    </requestFiltering>
  </security>
</system.webServer>

规则:

  • maxRequestLength(以kb表示)值必须匹配 maxAllowedContentLength(以字节表示)。
  • 大多数情况下,你的system.web部分可能已包含&#34; httpRuntime&#34;。将targetFramework设置为您使用的.net版本。

注意:

  • maxRequestLength的默认值为4096(4mb)。最大值为2,147,483,647
  • maxAllowedContentLength的默认值为30,000,000(约30mb)。最大值为4,294,967,295

更多信息MSDN

答案 10 :(得分:5)

如果您要求访问站点中的应用程序,请确保在根web.config中设置maxRequestLength。应用程序的web.config中的maxRequestLength似乎被忽略。

答案 11 :(得分:1)

由于我们的web.config文件有多个system.web部分,我被绊倒了:当我添加&lt; httpRuntime maxRequestLength =“1048576”/&gt;到配置级别的system.web部分。

答案 12 :(得分:1)

我必须编辑C:\Windows\System32\inetsrv\config\applicationHost.config文件并将<requestLimits maxAllowedContentLength="1073741824" />添加到...的末尾。

<configuration>
    <system.webServer>
        <security>
            <requestFiltering>

部分。

根据This Microsoft Support Article

答案 13 :(得分:0)

我正在处理相同的错误,并花了一些时间通过在web.config文件中添加以下行来解决它

<system.web>
   <httpRuntime targetFramework="4.7.1" maxRequestLength="1048576"/>
</system.web>

 <system.webServer>
   <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="1073741824" />
      </requestFiltering>
    </security>
</system.webServer>

答案 14 :(得分:-3)

我可以添加配置网络未编译

<system.web> 
  <httpRuntime maxRequestLength="1024" executionTimeout="3600" /> 
  <compilation debug="true"/> 
</system.web> 
<security> 
  <requestFiltering> 
    <requestLimits maxAllowedContentLength="1048576"/> 
  </requestFiltering> 
</security>
相关问题