随笔 - 139, 文章 - 5, 评论 - 39, 阅读 - 16万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 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

如何自定义上传文件大小限制

Posted on   小飞龙(Jack)  阅读(1096)  评论(3编辑  收藏  举报

大多数网站都具备上传文件的功能,拥护可以利用此功能从客户端向服务器上传文件.默认情况下,上传文件的大小不能超过4MB.如果超过此界限边不能上传.  那么如何自定义上传文件限制呢?

解答:

asp.net通过修改Web.config和Machine.config文件对网站和网站目录进行配制.

1)修改Web.config文件

  在web.config文件中添加<httpRuntime/>配制可以自定义上传文件大小限制.添加设置代码如下.

  <configuration>

    <system.web>

      <httpRuntime

          maxRequestLength="4096"  //此大小为默认值,此值可以根据需要改变

          executionTimeout="600"   //此值指定上传文件的有效时间为10分钟

        />

    </system.web>

 </configuration>

2) 修改Machine.config文件

  在Microsoft.NET\Framework\v1.1.4322\CONFIG目录下找到Machine.config文件.打开Machine.config文件可以看到如下设置代码.

  <!--
        httpRuntime Attributes:
          executionTimeout="[seconds]" - time in seconds before request is automatically timed out
          maxRequestLength="[KBytes]" - KBytes size of maximum request length to accept
          useFullyQualifiedRedirectUrl="[true|false]" - Fully qualifiy the URL for client redirects
          minFreeThreads="[count]" - minimum number of free thread to allow execution of new requests
          minLocalRequestFreeThreads="[count]" - minimum number of free thread to allow execution of new local requests
          appRequestQueueLimit="[count]" - maximum number of requests queued for the application
          enableKernelOutputCache="[true|false]" - enable the http.sys cache on IIS6 and higher - default is true
          enableVersionHeader="[true|false]" - outputs X-AspNet-Version header with each request
        -->
        <httpRuntime
            executionTimeout="90"
            maxRequestLength="4096"
            useFullyQualifiedRedirectUrl="false"
            minFreeThreads="8"
            minLocalRequestFreeThreads="4"
            appRequestQueueLimit="100"
            enableVersionHeader="true"
        />

上面的代码中executionTimeout属性用于指定上传操作的有效时间(单位秒).maxRequestLengh属性用于指定上传文件的最大字节,单位KB,此属性默认大小为4096KB(4MB).

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示