我有一个ASP.NET表单可以上传文件。我需要将最大上传大小增加到4 MB默认值以上。
我在某些地方找到了以下代码的引用,来自于msdn。
[ConfigurationPropertyAttribute("maxRequestLength", DefaultValue = )]
没有任何参考资料描述如何使用它,我已经尝试了几种方法但都没有成功。我只想修改某些要求文件上传的页面的属性。
这是正确的方法吗?如何使用它?
我有一个ASP.NET表单可以上传文件。我需要将最大上传大小增加到4 MB默认值以上。
我在某些地方找到了以下代码的引用,来自于msdn。
[ConfigurationPropertyAttribute("maxRequestLength", DefaultValue = )]
没有任何参考资料描述如何使用它,我已经尝试了几种方法但都没有成功。我只想修改某些要求文件上传的页面的属性。
这是正确的方法吗?如何使用它?
这个设置需要放在你的web.config文件中。它会影响整个应用程序,但我认为你不能针对每个页面进行设置。
<configuration>
<system.web>
<httpRuntime maxRequestLength="xxx" />
</system.web>
</configuration>
"xxx"是以KB为单位的。默认值为4096(= 4 MB)。
对于IIS 7+,除了添加httpRuntime maxRequestLength设置之外,您还需要添加:
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="52428800" /> <!--50MB-->
</requestFiltering>
</security>
</system.webServer>
或在IIS(7)中:
- 选择要启用大文件上传的网站。
- 在主窗口中双击“请求筛选”。
- 选择“编辑功能设置”。
- 修改“允许的最大内容长度(字节)”。
maxRequestLength
和maxAllowedContentLength
。请参阅https://dev59.com/P2w15IYBdhLWcg3w6f80。 - MikeM为了增加上传文件的大小限制,我们有两种方法
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
中会设置最大上传大小:
<system.web>
<httpRuntime maxRequestLength="600000"/>
</system.web>
对于最大限制为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>
编辑:我没有正确阅读问题(如何在webconfig中设置maxrequest)。但是这些信息可能对其他人有用,许多将其网站从win2000-server移动到win2003并具有工作上传功能,突然遇到Request.BinaryRead Failed错误的人会使用它。所以我把答案留在这里。
最大文件大小可以限制为单个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中添加这些条目。
我在一个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是解决问题的必要条件。
我知道这是一个老问题。
因此,您需要执行以下操作:
在您的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>
<httpRuntime targetFramework="4.6.1" requestValidationMode="2.0" maxRequestLength="10485760" />