標籤:blog http 使用 檔案 width io
在 ASP.NET 開發的過程中,檔案上傳往往使用內建的 FileUpload 控制項,可是用過的人都知道,這個控制項的局限性十分大,最大的問題就在於上傳大檔案時讓開發人員尤為的頭疼,而且,上傳時無法方便的做到多線程的操控和上傳進度的顯示。在此給大家推薦一款簡單易用的上傳組件,從而快速便捷得解決了 ASP.NET 中的大檔案上傳問題。
首先,我們需要這個名為 RanUpLoad 的組件(下面例子中附帶),這兩個 dll 檔案添加到項目的引用中區,xml 檔案也要複製在項目中的 bin 檔案夾下,也就是最後三個檔案都要存在於 bin 檔案夾中。
接著,上傳控制項還是用 ASP.NET 中內建的 FileUpload 控制項,需要添加的就是在 FileUpload 控制項旁邊加入標籤:
C#代碼
- <radU:RadProgressManager ID="Radprogressmanager1" Width="100%" runat="server" />
- <radU:RadProgressArea ID="progressArea1" Width="100%" runat="server"></radU:RadProgressArea>
並且在 前台頁面aspx 檔案的起始處添加如下代碼:
C#代碼
- <form id="form1" runat="server">
- <div>
- <radU:RadProgressManager ID="Radprogressmanager1" Width="100%" runat="server" />
- <radU:RadProgressArea ID="progressArea1" Width="100%" runat="server">
- </radU:RadProgressArea>
- <asp:FileUpload runat="server" ID="fileUPload" />
-
- <asp:Button runat="server" ID="fileUpladBtn" Text="上傳" OnClick="fileUpladBtn_OnClick" />
- </div>
- </form>
當然,設定檔的 <system.web> 標籤中不能忘記下面這些語句:
C#代碼
- <!--檔案上傳控制項配置-->
- <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>
下面就是後台檔案上傳操作
C#代碼
- protected void fileUpladBtn_OnClick(object sender, EventArgs e)
- {
- 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("~/Files", fileName);
- string savePath = this.MapPath(virtualPath);
- file.SaveAs(savePath, true);
- }
- BigFileUpload.rar (236.2 KB)