如何在运行于IIS7时将maxAllowedContentLength设置为500MB?

119

我把 maxAllowedContentLength 改成了

<security>
    <requestFiltering>
        <requestLimits maxAllowedContentLength="5024000000" />
    </requestFiltering>
</security>

在我的web.config文件中,但在运行IIS7时出现以下错误:

'maxAllowedContentLength'属性无效。不是有效的无符号整数。

http://i.stack.imgur.com/u1ZFe.jpg

但是,在VS服务器上运行时,它可以正常运行而没有任何错误。

如何配置我的网站以允许上传大小为500MB的文件,而不会在IIS7上出现此问题?


2
5024000000(让我添加千位分隔符)5.024.000.000大于最大的无符号整数4.294.967.295,您应该在配置中使用502.400.000作为值(不带千位分隔符)。 - Lennart
3个回答

202

.Net中的请求限制可以通过两个属性进行配置:

第一个

  • Web.Config/system.web/httpRuntime/maxRequestLength(以千字节为单位)
  • 单位:千字节
  • 默认值4096 KB(4 MB)
  • 最大值2147483647 KB(2 TB;旧版IIS可能限制为2097151 KB,即约2 GB)

第二个

  • Web.Config/system.webServer/security/requestFiltering/requestLimits/maxAllowedContentLength(以字节为单位)
  • 单位:字节
  • 默认值30000000字节(28.6 MB)
  • 最大值4294967295字节(4 GB)

参考资料:

例子:

<location path="upl">
   <system.web>
     <!--The default size is 4096 kilobytes (4 MB). MaxValue is 2147483647 KB (2 TB)-->
     <!-- 100 MB in kilobytes -->
     <httpRuntime maxRequestLength="102400" />
   </system.web>
   <system.webServer>
     <security>
       <requestFiltering>          
         <!--The default size is 30000000 bytes (28.6 MB). MaxValue is 4294967295 bytes (4 GB)-->
         <!-- 100 MB in bytes -->
         <requestLimits maxAllowedContentLength="104857600" />
       </requestFiltering>
     </security>
   </system.webServer>
 </location>

8
非常有帮助,但我认为maxAllowedContentLength的最大值大约是4 GB,而不是4 TB。 - Snicklefritz
该文章指出:“指定请求中内容的最大长度,以字节为单位。” 这意味着两个配置键都使用字节,使得最大请求大小相同,为4GB。 - abatishchev
@abatishchev 另一个配置键,即 httpRuntime maxRequestLength,以千字节为单位指定,而不是字节。requestLimits maxAllowContentLength 则仅以字节为单位指定。 - clamum
这对我起了作用。谢谢。 - Eccaos

105
根据 MSDNmaxAllowedContentLength 的类型为 uint,其 最大值 为 4,294,967,295 字节 = 3.99 GB。
因此应该可以正常工作。
另请参阅 请求限制文章。如果未完全配置适当的部分,IIS 是否会返回这些错误之一?
另请参阅:超出最大请求长度

所以我手头的这个数值5024000000是以GB为单位的吗? - Amr Elgarhy
2
500MB = 524288000,现在小于4294967295。 - Amr Elgarhy

20

IIS v10 (但对于 IIS 7.x 也应该是相同的)

针对正在寻找相应最大值的人的快速添加

maxAllowedContentLength 的最大值为:UInt32.MaxValue 4294967295 字节约为 4GB

maxRequestLength 的最大值为:Int32.MaxValue 2147483647 字节约为 2GB

web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <!-- ~ 2GB -->
    <httpRuntime maxRequestLength="2147483647" />
  </system.web>
  <system.webServer>
    <security>
      <requestFiltering>
        <!-- ~ 4GB -->
        <requestLimits maxAllowedContentLength="4294967295" />
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>

1
更改“httpRuntime maxRequestLength”不是一个好的做法(DoS),您需要更改的是“requestLimits maxAllowedContentLength”配置aspnet和iis请求长度以获取post数据 - Javier Cañon
1
-1 表示错误使用 maxRequestLength。它以千字节为单位指定,请参见 https://learn.microsoft.com/en-us/dotnet/api/system.web.configuration.httpruntimesection.maxrequestlength?view=netframework-4.8 - balintn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接