1.在工具箱中添加Brettle.Web.NeatUpload.dll,可以看到工具箱中出現InputFile等控制項。
2.複製檔案夾NeatUpload到根目錄.
3.拖放使用上傳控制項InputFile和進度條ProgressBar,添加上傳檔案的按鈕.aspx頁面代碼如下:
複製代碼 代碼如下:<body>
<form id="form1" runat="server">
<div>
<upload:InputFile id="AttachFile" runat="server"></upload:InputFile>
<asp:Button ID="Upload" runat="server" Text="Upload" OnClientClick="ToggleVisibility('ProgressBar','on')" OnClick="Upload_Click"/>
<div id="ProgressBar" style="display:none;">
<upload:progressbar id="pbProgressBar" runat="server" Inline="true" Width="500px" Height="100"> </upload:progressbar>
</div>
</div>
</form>
</body>
4.添加指令碼代碼ToggleVisiblity函數到頁面上如下: 複製代碼 代碼如下:<script type="text/javascript" language="javascript">
function ToggleVisibility(id, type)
{
el = document.getElementById(id);
if(el.style)
{
if(type == 'on')
{
el.style.display = 'block';
}
else
{
el.style.display = 'none';
}
}
else
{
if(type == 'on')
{
el.display = 'block';
}
else
{
el.display = 'none';
}
}
}
</script>
5.給上傳檔案的按鈕添加事件Upload_Click
複製代碼 代碼如下:protected void Upload_Click(object sender, EventArgs e)
{
string FileName = this.AttachFile.FileName;//擷取上傳檔案的全路徑
string ExtenName = System.IO.Path.GetExtension(FileName);//擷取副檔名
string SaveFileName = System.IO.Path.Combine(Request.PhysicalApplicationPath, DateTime.Now.ToString("yyyyMMddhhmm") + ExtenName);//合并兩個路徑為上傳到伺服器上的全路徑
if (this.AttachFile.ContentLength > 0)
{
try
{
this.AttachFile.MoveTo(SaveFileName, Brettle.Web.NeatUpload.MoveToOptions.Overwrite);
}
catch (Exception ex)
{
throw ex;
}
}
}
6.在設定檔web.config中添加http模組聲明: 複製代碼 代碼如下:<httpModules>
<add name="UploadHttpModule" type="Brettle.Web.NeatUpload.UploadHttpModule, Brettle.Web.NeatUpload" />
</httpModules>
7.需要在web.config進行其他一些設定才能保證完美。
在<configuration>裡面加入 複製代碼 代碼如下:<configSections>
<sectionGroup name="system.web">
<section name="neatUpload" type="Brettle.Web.NeatUpload.ConfigSectionHandler, Brettle.Web.NeatUpload" allowLocation="true" />
</sectionGroup>
</configSections>
在 <system.web>加入 複製代碼 代碼如下:<neatUpload useHttpModule="false" maxNormalRequestLength="4096" maxRequestLength="2097151" defaultProvider="FilesystemUploadStorageProvider">
<providers>
<add name="FilesystemUploadStorageProvider" type="Brettle.Web.NeatUpload.FilesystemUploadStorageProvider, Brettle.Web.NeatUpload" />
</providers>
</neatUpload>
在<configuration>裡面的後面位置加入 複製代碼 代碼如下:<location path="Default.aspx" >
<system.web>
<neatUpload useHttpModule="true" />
<httpRuntime maxRequestLength="2097151" executionTimeout="3600" useFullyQualifiedRedirectUrl="true" />
</system.web>
</location>
</configuration>
地址下載,裡面有詳細的使用文檔