我有一个窗体,除了一个文件上传在ASP.NET。我需要将最大上传大小增加到默认的4 MB以上。

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

[ConfigurationPropertyAttribute("maxRequestLength", DefaultValue = )]

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

走这条路对吗?我怎么使用这个呢?


当前回答

如果是windows 2003 / IIS 6.0,那么在C:\windows\system32\inetsrv\目录下的metbase .xml文件中检查AspMaxRequestEntityAllowed = "204800"

默认值“204800”(~205Kb)在我看来对于大多数用户来说太低了。只需将值更改为您认为应该是max的值。

如果你不能保存文件后编辑它,你必须停止ISS-server或使服务器允许编辑文件:

(来源:itmaskinen.se)

编辑:我没有阅读正确的问题(如何设置webconfig中的maxrequest)。但是这一信息可能会引起其他人的兴趣,许多人将他们的网站从win2000-server迁移到win2003,并且有一个正常的上传功能,然后突然收到了请求。BinaryRead失败错误将使用它。所以我把答案留在这里。

其他回答

对于2 GB的最大限制,在你的应用程序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>

这个设置在你的网页中。配置文件。不过,它会影响整个应用程序……我认为你不能每页设置。

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

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

我知道这是个老问题。

这就是你要做的:

在你的网上。配置文件,在<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>

你可以在评论中看到它是如何工作的。在其中一个中,sie的单位是字节,在另一个中,sie的单位是千字节。希望这能有所帮助。

最大文件大小可以限制为单个MVC控制器甚至一个动作。 网络。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中添加这些条目。

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

1. IIS6或更低

默认情况下,在ASP。上传到服务器的文件的最大大小为 4 mb左右。属性可以增加该值 config中的maxRequestLength属性。 记住:maxrequestlengt的单位是KB

例如:如果你想限制上传15MB,设置maxRequestLength为“15360”(15 * 1024)。

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

2. IIS7或更高

这里使用的上传文件的方式略有不同。IIS7已经 介绍了请求过滤模块。之前执行过 意思是流水线的工作方式是IIS value(maxAllowedContentLength)先检查,然后ASP。网 value(maxRequestLength)被检查。的maxAllowedContentLength 属性默认为28.61 mb。此值可以通过 在同一个web.config中修改两个属性。 记住:maxAllowedContentLength以字节为单位

例如:如果你想限制上传15MB,设置maxRequestLength为“15360”,maxAllowedContentLength为“15728640”(15 * 1024 * 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