『QQ:1353814576』

Web上传文件请求报 “超过了最大请求长度”异常


开发过程中遇到"System.Web.HttpException: 超过了最大请求长度。"的问题

如下面图图片所示,IIS默认请求长度4M,当请求长度大于这个数值限制的时候就会报错,

下面是解决方案.

修改:在web.config 中修改<system.web></system.web>节点中的 maxRequestLength,表示最大请求长度,单位是kb,系统默认的是4M限制 超出就会拒绝接受 该限制可用于防止因用户将大量文件传递到该服务器而导致的拒绝服务攻击。

<system.web>
         <!--最大请求长度,单位为kb-->
         <httpRuntime maxRequestLength="20480"  />
</system.web>`

修改在web.config中修改 <system.webServer></system.webServer>节点中的 maxAllowedContentLength 表示附件大小上限,单位是字节,默认约30M?

 <system.webServer>
       <!--允许上传文件长度,单位字节-->
       <security>
          <requestFiltering>
           <requestLimits maxAllowedContentLength="20971520"/>
         </requestFiltering>
       </security>
</system.webServer>

maxRequestLength与maxAllowedContentLength的区别:

  1. maxRequestLength 表示请求长度,
  2. maxAllowedContentLength 表示上传文件的大小;
  3. maxRequestLength 单位kb,
  4. maxAllowedContentLength? 单位字节;
  5. maxRequestLength 默认值4M,
  6. maxAllowedContentLength 默认值30000000B,约30M;
  7. 最大值都为2G 是这两者的共同点