玩转C科技.NET

从学会做人开始认识这个世界!http://volnet.github.io

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

ASP.NET 上传文件最大值调整

首先,最容易找到的是web.config下面配置:

    <!--maxRequestLength=50MB-->
    <httpRuntime targetFramework="4.5.2" maxRequestLength="51200"/>

这么设置会将请求的尺寸从默认4MB(4096KB)提升到50MB(51200KB)。

但是,如果只是这么设置的话,你会发现你的最大上传尺寸会停止在28.6MB,更大的文件上传,将返回404.13,表示内容长度过大。

原因在于IIS的默认设置,限定了maxAllowedContentLength的值。

<element name="requestLimits"><attribute name="maxAllowedContentLength" type="uint" defaultValue="30000000" />

该值在IIS_Schema.xml中设置。(将影响整个计算机)

IISExpress:IISExpress运行路径下,C:\Program Files (x86)\IIS Express\config\schema\IIS_schema.xml
IIS7、8:C:\Windows\System32\inetsrv\config\schema\IIS_schema.xml

如果修改了这个值没有生效,两种可能,一种是当前运行的IISExpress/IIS没有关闭/重启。一种是web.config中有额外的配置(如下文所述)。

 

然而为了避免总是去修改全局的值,影响到同一台服务器上的其他web站点,我们还可以在web.config中进行配置。

复制代码
  <system.webServer>
    <!--
      IISExpress:C:\Users\UserName\Documents\IISExpress\config\applicationHost.config
      IIS7、8:C:\Windows\System32\inetsrv\config\applicationHost.config
      <section name="requestFiltering" overrideModeDefault="Allow" />
      overrideModeDefault值设置成Allow的时候,本配置节才会生效。
      
      也可以在IIS_Schema.xml中设置。(将影响整个计算机)
      IISExpress:IISExpress运行路径下,C:\Program Files (x86)\IIS Express\config\schema\IIS_schema.xml
      IIS7、8:C:\Windows\System32\inetsrv\config\schema\IIS_schema.xml
      修改defaultValue="30000000"(28.6MB)为需要的值。
      <element name="requestLimits"><attribute name="maxAllowedContentLength" type="uint" defaultValue="30000000" />
    -->
    <security>
      <requestFiltering>
        <!--maxAllowedContentLength=100MB-->
        <requestLimits maxAllowedContentLength="104857600"></requestLimits>
      </requestFiltering>
    </security>
复制代码

为了使该内容生效,需要配置“允许继承”,在applicationHost.config中搜索<section name="requestFiltering" overrideModeDefault="Allow" />中的关键字,确保这个值为Allow即可。

IISExpress:C:\Users\UserName\Documents\IISExpress\config\applicationHost.config
IIS7、8:C:\Windows\System32\inetsrv\config\applicationHost.config

 

参考资料:

http://www.iis.net/configreference/system.webserver/security/requestfiltering/requestlimits

http://www.cnblogs.com/henryhappier/archive/2010/09/20/1832098.html

https://msdn.microsoft.com/en-us/library/ms689462(v=vs.90).aspx

posted on   volnet(可以叫我大V)  阅读(1862)  评论(0编辑  收藏  举报

编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
使用Live Messenger联系我
点击右上角即可分享
微信分享提示