一般10M以下的文件上传通过设置Web.Config,再用VS自带的FileUpload控件就可以了,但是如果要上传100M甚至1G的文件就不能这样上传了,这时可以用NeatUpload来上传,步骤如下:
1,在工具箱中添加Brettle.Web.NeatUpload.dll,方法:打开VS--->点击'工具箱'--->选中一项,如:'常规'这项--->单击右键--->在弹出的菜 单中选'选择项'--->在弹出的窗口中选择'浏览'--->找到'Brettle.Web.NeatUpload.dll'这个文件 --->按确定就添加上了,这时工具箱中多出几个控件出来,有InputFile,progressbar 等,我们主要用这两个.
2.复制文件夹NeatUpload到根目录.
3.拖放使用上传控件InputFile和进度条ProgressBar,添加上传文件的按钮.aspx页面代码如下:
<!--注册-->
<%@ Register assembly="Brettle.Web.NeatUpload" namespace="Brettle.Web.NeatUpload" tagprefix="Upload" %>
<!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>
</head>
<body id="linkedcontent">
<form id="form1" runat="server">
<div class="pagebox">
<div class="innerbox">
<Upload:InputFile ID="<span style="font-family: Verdana, Arial, Helvetica, sans-serif;">AttachFile</span>" runat="server" />
<asp:Button ID="btnUpload" Text="上传" runat="server" οnclick="btnUpload_Click" />
<br />
<Upload:ProgressBar ID="ProgressBar1" runat="server" />
</div>
</div>
</form>
</body>
</html>
4.给上传按钮添加Upload_Click事件:
protected void Upload_Click(object sender, EventArgs e)
{
if (AttachFile.HasFile)
{
string FileName = this.AttachFile.FileName;//获取上传文件的文件名,包括后缀
string ExtenName = System.IO.Path.GetExtension(FileName);//获取扩展名
string SaveFileName = System.IO.Path.Combine(
System.Web.HttpContext.Current.Request.MapPath("UpLoads/"),
DateTime.Now.ToString("yyyyMMddhhmm") + ExtenName);//合并两个路径为上传到服务器上的全路径
AttachFile.MoveTo(SaveFileName, Brettle.Web.NeatUpload.MoveToOptions.Overwrite);
string url = "UpLoads/" + DateTime.Now.ToString("yyyyMMddhhmmss") + ExtenName; //文件保存的路径
float FileSize = (float)System.Math.Round((float)AttachFile.ContentLength / 1024000, 1); //获取文件大小并保留小数点后一位,单位是M
}
}
5.注意,要还在Web.Config里进行设置:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="system.web">
<section name="neatUpload"
type="Brettle.Web.NeatUpload.ConfigSectionHandler,Brettle.Web.NeatUpload" allowLocation="true" />
</sectionGroup>
</configSections>
<system.web>
<httpModules>
<!--上传大文件-->
<add name="UploadHttpModule" type="Brettle.Web.NeatUpload.UploadHttpModule,Brettle.Web.NeatUpload"/>
</httpModules>
<!--useHttpModule="true" 设为true时才能看到上传状态,默认是False-->
<neatUpload useHttpModule="true" maxNormalRequestLength="1048576" maxRequestLength="1048576" defaultProvider="FilesystemUploadStorageProvider">
<providers>
<add name="FilesystemUploadStorageProvider"
type="Brettle.Web.NeatUpload.FilesystemUploadStorageProvider, Brettle.Web.NeatUpload"/>
</providers>
</neatUpload>
<httpRuntime maxRequestLength="1048576" executionTimeout="3600"/>
<compilation debug="true" targetFramework="4.0" />
</system.web>
</configuration>