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

时间:2008-11-13 22:44:04

标签: c# .net asp.net file-upload

我有一个表单,除了ASP.NET中的文件上传。我需要将最大上传大小增加到4 MB以上。

我在某些地方找到了引用msdn下面代码的地方。

[ConfigurationPropertyAttribute("maxRequestLength", DefaultValue = )]

所有参考文献都没有真正描述如何使用它,我尝试了几件事但没有成功。我只想为要求上传文件的某些页面修改此属性。

这是正确的路线吗?我该如何使用它?

14 个答案:

答案 0 :(得分:377)

此设置包含在您的web.config文件中。它会影响整个应用程序,但是......我认为你不能每页设置它。

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

“xxx”以KB为单位。默认值为4096(= 4 MB)。

答案 1 :(得分:163)

对于IIS 7+,以及添加httpRuntime maxRequestLength设置,您还需要添加:

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

或者在IIS(7)中:

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

答案 2 :(得分:65)

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

<强> 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.Means管道工作的方式是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

答案 3 :(得分:15)

我相信web.config中的这一行会设置最大上传大小:

<system.web>

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

答案 4 :(得分: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失败错误将使用它。所以我在这里留下答案。

答案 5 :(得分:4)

我在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是解决问题的必要条件。

答案 6 :(得分:3)

我知道这是一个老问题。

所以这就是你要做的事情:

在您的web.config文件中,将其添加到:

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

,在

<security>
    <requestFiltering>

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

    </requestFiltering>
</security>

你在评论中看到它是如何工作的。在一个中你需要以字节为单位,而另一个以千字节为单位。希望有所帮助。

答案 7 :(得分:3)

如果您使用的是Framework 4.6

<httpRuntime targetFramework="4.6.1" requestValidationMode="2.0" maxRequestLength="10485760"  />

答案 8 :(得分:2)

您可以在应用程序web.config文件中编写该代码块。

<httpRuntime maxRequestLength="2048576000" />
<sessionState timeout="3600"  />

通过编写该代码,您可以上传比现在更大的文件

答案 9 :(得分:0)

如果您使用sharepoint,您还应该使用管理工具配置最大大小: kb925083

答案 10 :(得分:0)

我有一篇关于如何increase the file size for asp upload control的博客文章。

来自帖子:

  

默认情况下,FileUpload控件允许上传和执行最多4MB的文件    超时是110秒。可以在web.config文件的httpRuntime部分中更改这些属性。 maxRequestLength属性确定可以上载的最大文件大小。该    executionTimeout属性确定执行的最长时间。

答案 11 :(得分:0)

如果它在您的本地计算机上工作但在IIS中部署后不起作用(我使用的是Windows Server 2008 R2),我有一个解决方案。

打开IIS(inetmgr) 转到您的网站 在右侧转到内容(请求过滤) 转到编辑功能设置 将最大内容大小更改为(您需要的字节数) 这会奏效。 您也可以从以下主题获取帮助 http://www.iis.net/configreference/system.webserver/security/requestfiltering/requestlimits

答案 12 :(得分:0)

最大文件大小可以限制为单个MVC控制器,甚至可以限制为一个动作。
web.config 标签可用于此目的:

<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中添加这些条目。

答案 13 :(得分:0)

如果使用ssl cert,则要配置ssl管道F5设置。重要

相关问题