ASP.NET 大檔案上傳的簡單處理

來源:互聯網
上載者:User

標籤:blog   http   使用   檔案   width   io   

在 ASP.NET 開發的過程中,檔案上傳往往使用內建的 FileUpload 控制項,可是用過的人都知道,這個控制項的局限性十分大,最大的問題就在於上傳大檔案時讓開發人員尤為的頭疼,而且,上傳時無法方便的做到多線程的操控和上傳進度的顯示。在此給大家推薦一款簡單易用的上傳組件,從而快速便捷得解決了 ASP.NET 中的大檔案上傳問題。

首先,我們需要這個名為 RanUpLoad 的組件(下面例子中附帶),這兩個 dll 檔案添加到項目的引用中區,xml 檔案也要複製在項目中的 bin 檔案夾下,也就是最後三個檔案都要存在於 bin 檔案夾中。

 

接著,上傳控制項還是用 ASP.NET 中內建的 FileUpload 控制項,需要添加的就是在 FileUpload 控制項旁邊加入標籤:

C#代碼  
  1. <radU:RadProgressManager ID="Radprogressmanager1" Width="100%" runat="server" />  
  2. <radU:RadProgressArea ID="progressArea1" Width="100%" runat="server"></radU:RadProgressArea>  

 並且在 前台頁面aspx 檔案的起始處添加如下代碼:

C#代碼  
  1. <form id="form1" runat="server">  
  2.     <div>  
  3.         <radU:RadProgressManager ID="Radprogressmanager1" Width="100%" runat="server" />  
  4.         <radU:RadProgressArea ID="progressArea1" Width="100%" runat="server">  
  5.         </radU:RadProgressArea>  
  6.         <asp:FileUpload runat="server" ID="fileUPload" />  
  7.         &nbsp;&nbsp;&nbsp;  
  8.         <asp:Button runat="server" ID="fileUpladBtn" Text="上傳" OnClick="fileUpladBtn_OnClick" />  
  9.     </div>  
  10.     </form>  

 

 當然,設定檔的 <system.web> 標籤中不能忘記下面這些語句:

C#代碼  
  1. <!--檔案上傳控制項配置-->  
  2.    <httpRuntime executionTimeout="3600" maxRequestLength="2097151" ></httpRuntime>  
  3.    <httpModules>  
  4.      <add name="RadUploadModule" type="Telerik.WebControls.RadUploadHttpModule, RadUpload.Net2"/>  
  5.    </httpModules>  
  6.    <httpHandlers>  
  7.      <add verb="*" path="Telerik.RadUploadProgressHandler.aspx" type="Telerik.WebControls.RadUploadProgressHandler, RadUpload.Net2"></add>  
  8.    </httpHandlers>  

 下面就是後台檔案上傳操作

C#代碼  
  1. protected void fileUpladBtn_OnClick(object sender, EventArgs e)  
  2.         {  
  3.             if (RadUploadContext.Current == null) { return; }  
  4.             if (RadUploadContext.Current.UploadedFiles.Count <= 0)  
  5.             {  
  6.                 this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "MsgBox", "<script>alert(‘請選擇上傳檔案 !‘)</script>");  
  7.                 return;  
  8.             }  
  9.             if (RadUploadContext.Current.UploadedFiles[0].ContentLength >= 2147483647)  
  10.             {  
  11.                 this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "MsgBox", "<script>alert(‘上傳的檔案不得超過 2GB !‘)</script>");  
  12.                 return;  
  13.             }  
  14.             UploadedFile file = RadUploadContext.Current.UploadedFiles[0];  
  15.             string fileName = Path.GetFileName(file.FileName);  
  16.             string virtualPath = System.IO.Path.Combine("~/Files", fileName);  
  17.             string savePath = this.MapPath(virtualPath);  
  18.             file.SaveAs(savePath, true);   
  19.         }  

 

  • BigFileUpload.rar (236.2 KB)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.