我有一个表单,除了 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) 中:
- 选择要启用以接受大文件上传的网站。
- 在主窗口中双击“请求过滤”
- 选择“编辑功能设置”
- 修改“最大允许内容长度(字节)”
为了增加上传文件的大小限制,我们有两种方法
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>
对于 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>
如果它的 windows 2003 / IIS 6.0 然后检查位于文件夹 C:\windows\system32\inetsrv\的文件metabase.xml中的 AspMaxRequestEntityAllowed = "204800"
在我看来,“204800”(~205Kb)的默认值对于大多数用户来说太低了。只需将值更改为您认为应该最大的值。
如果在编辑后无法保存文件,则必须停止 ISS 服务器或启用服务器以允许编辑文件:
(来源:itmaskinen.se)
编辑:我没有正确阅读问题(如何在 webconfig 中设置 maxrequest)。但是这个信息可能对其他人感兴趣,许多将他们的网站从win2000服务器移动到win2003并且有一个工作上传功能并且突然得到Request.BinaryRead Failed错误的人都会使用它。所以我把答案留在这里。
我在 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>
您在评论中看到这是如何工作的。在一个中,您需要以字节为单位,在另一个中以千字节为单位。希望有帮助。
最大文件大小可以限制为单个 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 中添加这些条目。
如果您使用的是框架 4.6
<httpRuntime targetFramework="4.6.1" requestValidationMode="2.0" maxRequestLength="10485760" />
您可以在应用程序 web.config 文件中编写该代码块。
<httpRuntime maxRequestLength="2048576000" />
<sessionState timeout="3600" />
通过编写该代码,您可以上传比现在更大的文件
如果您使用 sharepoint,您也应该使用管理工具配置最大大小: kb925083
我有一篇关于如何增加 asp 上传控制的文件大小的博文。
从帖子:
默认情况下,FileUpload 控件允许上传最大 4MB 的文件,执行超时为 110 秒。可以从 web.config 文件的 httpRuntime 部分更改这些属性。maxRequestLength 属性确定可以上传的最大文件大小。executionTimeout 属性确定执行的最长时间。
如果它在您的本地机器上工作并且在 IIS 中部署后不起作用(我使用 Windows Server 2008 R2),我有一个解决方案。
打开 IIS (inetmgr) 转到您的网站 在右侧转到内容(请求过滤) 转到编辑功能设置 将最大内容大小更改为(您需要的字节数)这将起作用。您还可以从以下线程 http://www.iis.net/configreference/system.webserver/security/requestfiltering/requestlimits获得帮助
如果您使用 ssl 证书,您的 ssl 管道 F5 设置配置。重要。