ASP.NET文件上传大小限制
我们大家都知道ASP.NET为我们提供了文件上传服务器控件FileUpload,默认情况下可上传的最大文件为4M,如果要改变可上传文件大小限制,那么我们可以在web.config中的httpRuntime元素中添加maxRequestLength属性设置大小,同时为了支持大文件上传超时可以添加executionTimeout属性设置超时时间。网上有很多这样的例子,但实际情况是否是这样吗?
<httpRuntime maxRequestLength="" executionTimeout=""/>
测试环境
IIS 7.5、.NET 3.5 sp1
测试页面UploadFile.aspx
复制代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UploadFile.aspx.cs" Inherits="UploadFile" %>
<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<p>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" />
</p>
</form>
</body>
</html>
复制代码
测试页面UploadFile.aspx.cs
复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class UploadFile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (FileUpload1.FileContent != null)
{
FileUpload1.SaveAs(Server.MapPath("/Files/"+FileUpload1.FileName));
}
}