首先,我們需要下載這個名為 RanUpLoad 的組件。
下載完成之後,兩個 dll 檔案添加到項目的引用中區,xml 檔案也要複製在項目中的 bin 檔案夾下,也就是最後三個檔案都要存在於 bin 檔案夾中。
接著,上傳控制項還是用 ASP.NET 中內建的 FileUpload 控制項,需要添加的就是在 FileUpload 控制項旁邊加入標籤:
<radU:RadProgressManager ID="Radprogressmanager1" Width="100%" runat="server" /><radU:RadProgressArea ID="progressArea1" Width="100%" runat="server"></radU:RadProgressArea>
並且在 aspx 檔案的起始處添加如下代碼:
<%@ Register TagPrefix="telerik" Namespace="Telerik.QuickStart" Assembly="Telerik.QuickStart" %><%@ Register TagPrefix="radU" Namespace="Telerik.WebControls" Assembly="RadUpload.Net2" %>
當然,設定檔的 <system.web> 標籤中不能忘記下面這些語句:
<httpRuntime executionTimeout="3600" maxRequestLength="2097151" ></httpRuntime><httpModules> <add name="RadUploadModule" type="Telerik.WebControls.RadUploadHttpModule, RadUpload.Net2"/></httpModules><httpHandlers> <add verb="*" path="Telerik.RadUploadProgressHandler.aspx" type="Telerik.WebControls.RadUploadProgressHandler, RadUpload.Net2"></add></httpHandlers>
現在,外部的輪廓都已經布好了,接下來就是點擊上傳之後伺服器端所需的操作:
當然,做這些操作之前,我們先 using 一下 Telerik.WebControls 命名空間。
// 檢查檔案if (RadUploadContext.Current == null) { return; }if (RadUploadContext.Current.UploadedFiles.Count <= 0) { this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "MsgBox", "<script>alert('請選擇上傳檔案 !')</script>"); return;}if (RadUploadContext.Current.UploadedFiles[0].ContentLength >= 2147483647){ this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "MsgBox", "<script>alert('上傳的檔案不得超過 2GB !')</script>"); return;}UploadedFile file = RadUploadContext.Current.UploadedFiles[0];string fileName = Path.GetFileName(file.FileName);string virtualPath = System.IO.Path.Combine("~/save", fileName);string savePath = this.MapPath(virtualPath);file.SaveAs(savePath, true);
至此,檔案上傳的處理工作已經完成,以上的cs代碼是我自己的一些操作處理,大家可以根據自己情況酌情修改,比如也可以放置多個FileUpload 控制項,
用foreach (UploadedFile file in RadUploadContext.Current.UploadedFiles){ ... } 這樣的方式處理多個檔案的上傳。
希望此篇文章可以協助對大檔案上傳頭疼的朋友們去輕鬆處理上傳問題。