如何在ASP.NET中增加最大上传文件大小?

270

我有一个ASP.NET表单可以上传文件。我需要将最大上传大小增加到4 MB默认值以上。

我在某些地方找到了以下代码的引用,来自于msdn

[ConfigurationPropertyAttribute("maxRequestLength", DefaultValue = )]

没有任何参考资料描述如何使用它,我已经尝试了几种方法但都没有成功。我只想修改某些要求文件上传的页面的属性。

这是正确的方法吗?如何使用它?


你确定这是代码限制,而不是主机限制吗?IIS 也有限制。 - MrChrister
我非常确定这是一个 .Net 的限制。下面的答案对我有用。 - Eddie
14个回答

447

这个设置需要放在你的web.config文件中。它会影响整个应用程序,但我认为你不能针对每个页面进行设置。

<configuration>
  <system.web>
    <httpRuntime maxRequestLength="xxx" />
  </system.web>
</configuration>

"xxx"是以KB为单位的。默认值为4096(= 4 MB)。


8
这让我开始为整个网站工作。 我现在将其设置为10240(或10 MB)。 谢谢! - Eddie
15
如果你正在运行IIS7+,而且这个方法不起作用的话,请查看我的回答。 - 4imble
能否将此限制在一个控制器中? - Guillermo Varini
2
这也可以在特定路径上完成。 <location path="Api/Controller"> <system.web> <httpRuntime maxRequestLength="102400" /> </system.web> </location> - nios
为什么我要限制上传大小呢? 如果我设置允许100GB会发生什么? - Web Developer
显示剩余2条评论

203

对于IIS 7+,除了添加httpRuntime maxRequestLength设置之外,您还需要添加:

  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="52428800" /> <!--50MB-->
      </requestFiltering>
    </security>
  </system.webServer>

或在IIS(7)中:

  • 选择要启用大文件上传的网站。
  • 在主窗口中双击“请求筛选”。
  • 选择“编辑功能设置”。
  • 修改“允许的最大内容长度(字节)”。

我忘记了千字节 :p - Andrew Myhre
11
为使其正常工作,您可能需要同时设置maxRequestLengthmaxAllowedContentLength。请参阅https://dev59.com/P2w15IYBdhLWcg3w6f80。 - MikeM
7
@AndrewMyhre maxAllowedContentLength表示允许的内容长度以字节为单位,而不是以KB为单位。微软文档称其默认值为30 MB。 - Dan Randolph
1
@DanRandolph 实际上,默认值是30000000(约为28.6MB),如文档所述:https://www.iis.net/configreference/system.webserver/security/requestfiltering/requestlimits - aaaantoine
你肯定需要这两个设置。 - RameyRoad
显示剩余2条评论

90

为了增加上传文件的大小限制,我们有两种方法

1. IIS6或更低版本

在ASP.Net中,默认情况下上传到服务器的文件的最大大小约为4MB。可以通过修改web.config中的maxRequestLength属性来增加此值。

注意:maxRequestLenght是以KB为单位的

示例:如果您想将上传限制为15MB,请将maxRequestLength设置为“15360”(15 x 1024)。

<system.web>
   <!-- maxRequestLength for asp.net, in KB --> 
   <httpRuntime maxRequestLength="15360" ></httpRuntime> 
</system.web>

2. IIS7或更高版本

这里使用了稍微不同的方式来上传文件。IIS7引入了请求筛选模块。它在ASP.Net之前执行。这意味着管道工作的方式是先检查IIS值(maxAllowedContentLength),然后再检查ASP.NET的值(maxRequestLength)。maxAllowedContentLength属性默认为28.61 MB。通过在同一个 web.config 中修改这两个属性可以增加该值。

记住:maxAllowedContentLength以字节为单位

示例:如果您想将上传限制为15MB,则将maxRequestLength设置为“15360”,将maxAllowedContentLength设置为"15728640"(15 x 1024 x 1024)。

<system.web>
   <!-- maxRequestLength for asp.net, in KB --> 
   <httpRuntime maxRequestLength="15360" ></httpRuntime> 
</system.web>

<system.webServer>              
   <security> 
      <requestFiltering> 
         <!-- maxAllowedContentLength, for IIS, in bytes --> 
         <requestLimits maxAllowedContentLength="15728640" ></requestLimits>
      </requestFiltering> 
   </security>
</system.webServer>

MSDN参考链接https://msdn.microsoft.com/en-us/library/e1f13641(VS.80).aspx


将您提供的代码添加到Web.config中是否会影响IIS(我的意思是,如果我更改Web.config中的值,是否会进行IIS重置)。我特别是在问IIS 7。 - Jimesh
是的,通常更改 web.config 会强制重置该应用程序的应用程序池(适用于我所知道的7个以上)。 - Richard Squires

20

我相信这行代码在Web.config中会设置最大上传大小:

<system.web>

        <httpRuntime maxRequestLength="600000"/>
</system.web>

在度过了半天的时间后,这就是帮助我的东西!!!非常感谢!!! - Sagar Khatri
如果我正确配置了您的代码,任何大小的文件都可以上传吗? 它也适用于IIS7吗?感谢您的帮助。 - Jimesh

20

对于最大限制为2GB的情况,在您的应用程序web.config文件中:

<system.web>
  <compilation debug="true" targetFramework="4.5" />
  <httpRuntime targetFramework="4.5" maxRequestLength="2147483647" executionTimeout="1600" requestLengthDiskThreshold="2147483647" />
</system.web>

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

7
如果您使用的是Windows 2003 / IIS 6.0,则请查看位于文件夹C:\ windows \ system32 \ inetsrv \中的 metabase.xml 文件中的AspMaxRequestEntityAllowed =“204800”。 在我看来,“204800”(约205Kb)的默认值对于大多数用户来说太低了。只需将该值更改为您认为应该达到的最大值即可。 如果您无法在编辑后保存文件,则必须停止ISS服务器或启用服务器以允许编辑该文件: alt text (来源:itmaskinen.se

编辑:我没有正确阅读问题(如何在webconfig中设置maxrequest)。但是这些信息可能对其他人有用,许多将其网站从win2000-server移动到win2003并具有工作上传功能,突然遇到Request.BinaryRead Failed错误的人会使用它。所以我把答案留在这里。


6

最大文件大小可以限制为单个MVC控制器甚至是一个操作。
可以使用web.config的<location>标签实现此功能:

<location path="YourAreaName/YourControllerName>/YourActionName>">
  <system.web>
    <!-- 15MB maxRequestLength for asp.net, in KB 15360 -->
    <httpRuntime maxRequestLength="15360" />
  </system.web>
  <system.webServer>
    <security>
      <requestFiltering>
        <!-- 15MB maxAllowedContentLength, for IIS, in bytes 15728640 -->
        <requestLimits maxAllowedContentLength="15728640" />
      </requestFiltering>
    </security>
  </system.webServer>
</location>

或者你可以在区域自己的web.config中添加这些条目。


这个应该更高效,而且比已接受的答案更安全,因为它可以验证请求大小。 - David Ortega

6

我在一个Win 2008 IIS服务器上有同样的问题,我通过在web.config中添加以下配置解决了这个问题:

<system.web>
    <httpRuntime executionTimeout="3600" maxRequestLength="102400" 
     appRequestQueueLimit="100" requestValidationMode="2.0"
     requestLengthDiskThreshold="10024000"/>
</system.web>

requestLengthDiskThreshold 默认值为80000字节,对于我的应用程序来说太小了。requestLengthDiskThreshold以字节为单位进行测量,而maxRequestLength以千字节表示。

如果应用程序使用System.Web.UI.HtmlControls.HtmlInputFile服务器组件,则存在问题。增加requestLengthDiskThreshold是解决问题的必要条件。


1
根据 https://msdn.microsoft.com/en-us/library/e1f13641(v=vs.100).aspx 的说明,“指定输入流缓冲阈值的限制,以千字节为单位。此值不应超过maxRequestLength属性。”因此它应该最多与请求长度相同? - Jeff
是的,@Jeff,requestLengthDiskThreshold的值应该小于maxRequestLength,但前者以字节为单位表示。如果requestLengthDiskThreshold大于maxRequestLength,则应抛出ConfigurationErrorsException异常,因此您可以自行测试正确的值。请参见http://forums.asp.net/t/1680176.aspx?httpRuntime+maxRequestLength+vs+requestLengthDiskThreshold+。 - Massimo Zerbini

5

我知道这是一个老问题。

因此,您需要执行以下操作:

在您的web.config文件中,在<system.web>中添加以下内容:

<!-- 3GB Files / in kilobyte (3072*1024) -->
<httpRuntime targetFramework="4.5" maxRequestLength="3145728"/>

还有这个在<system.webServer>下的:

<security>
    <requestFiltering>

      <!-- 3GB Files / in byte (3072*1024*1024) -->
      <requestLimits maxAllowedContentLength="3221225472" />

    </requestFiltering>
</security>

你可以看到评论中的内容。在一个中,你需要以字节为单位,在另一个中则是以千字节为单位。希望对您有所帮助。

4
如果您正在使用4.6框架
<httpRuntime targetFramework="4.6.1" requestValidationMode="2.0" maxRequestLength="10485760"  />

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