我們大家都知道ASP.NET為我們提供了文件上傳服務器控件FileUpload,默認情況下可上傳的最大文件為4M,如果要改變可上傳文件大小限制,那么我們可以在web.config中的httpRuntime元素中添加maxRequestLength屬性設置大小,同時為了支持大文件上傳超時可以添加executionTimeout屬性設置超時時間。網上有很多這樣的例子,但實際情況是否是這樣嗎?
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UploadFile.aspx.cs" Inherits="UploadFile" %> <!DOCTYPE html> <html xmlns="http://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"> <div> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="Button1" runat="server" Text="Button" /> </div> </form> </body> </html>
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)); } } }
3. 修改web.config文件,設置可上傳文件大小限制
在 System.Web.HttpRequest.GetEntireRawContent() 在 System.Web.HttpRequest.GetMultipartContent() 在 System.Web.HttpRequest.FillInFormCollection() 在 System.Web.HttpRequest.EnsureForm() 在 System.Web.HttpRequest.get_HasForm() 在 System.Web.UI.Page.GetCollectionBasedOnMethod(Boolean dontReturnNull) 在 System.Web.UI.Page.DeterminePostBackMode() 在 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)