ASP.NET對大檔案上傳的解決方案_實用技巧

來源:互聯網
上載者:User

首先,我們需要下載這個名為 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){ ... }  這樣的方式處理多個檔案的上傳。

希望此篇文章可以協助對大檔案上傳頭疼的朋友們去輕鬆處理上傳問題。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.