上传大文件首先要修改web.config文件,否则上传报错。在web.config添加如下配置maxRequestLength表示能上传的最大文件值,单位是KB,requestLengthDiskThreshold表示超过多少KB之后的文件缓存到文件系统,不缓存在内存,以减轻内存负担。requestLengthDiskThreshold必须小于maxRequestLength
<configuration> <system.web> <httpRuntime maxRequestLength ="1048576" requestLengthDiskThreshold ="100"/> </system.web> </configuration>
上传页面
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebFormLargeFile.aspx.cs" Inherits="WebApplication1.WebFormLargeFile" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <style type ="text/css" > .fileList { margin-bottom :5px; } </style> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID ="lblFile" runat ="server" AssociatedControlID ="upFile" Text ="World Document:"></asp:Label> <asp:FileUpload ID ="upFile" runat ="server" /> <asp:Button ID ="btnAdd" runat ="server" onclick="btnAdd_Click" Text ="上传" /> <hr /> <asp:Repeater ID ="rptFiles" runat ="server" DataSourceID ="srcFiles" > <HeaderTemplate > <ul class ="fileList"> </HeaderTemplate> <ItemTemplate > <li> <asp:HyperLink ID ="lnkFile" runat ="server" Text ='<%#Eval("FileName") %>' NavigateUrl ='<%#Eval("Id","~/FileHandlerLarge.ashx?Id={0}") %>'></asp:HyperLink> </li> </ItemTemplate> <FooterTemplate > </ul> </FooterTemplate> </asp:Repeater> </div> <asp:SqlDataSource ID="srcFiles" runat="server" ConnectionString="Data Source=localhost;Initial Catalog=test;Integrated Security=True" ProviderName ="System.Data.SqlClient" SelectCommand="SELECT * FROM [Files]"></asp:SqlDataSource> </form> </body> </html>