大多数网站都具备上传文件的功能,拥护可以利用此功能从客户端向服务器上传文件.默认情况下,上传文件的大小不能超过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).
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .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 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义