在 Controller 中我們可以使用 FileResult 向用戶端傳送檔案。
FileResult
FileResult 是一個抽象類別,繼承自 ActionResult。在 System.Web.Mvc.dll 中,它有如上三個子類,分別以不同的方式向用戶端傳送檔案。
在實際使用中我們通常不需要直接執行個體化一個 FileResult 的子類,因為 Controller 類已經提供了六個 File 方法來簡化我們的操作:
protected internal FilePathResult File(string fileName, string contentType); protected internal virtual FilePathResult File(string fileName, string contentType, string fileDownloadName); protected internal FileContentResult File(byte[] fileContents, string contentType); protected internal virtual FileContentResult File(byte[] fileContents, string contentType, string fileDownloadName); protected internal FileStreamResult File(Stream fileStream, string contentType); protected internal virtual FileStreamResult File(Stream fileStream, string contentType, string fileDownloadName); |
FilePathResult
FilePathResult 直接將磁碟上的檔案發送至瀏覽器:
1. 最簡單的方式
public ActionResult FilePathDownload1() { var path = Server.MapPath("~/Files/鶴衝天.zip"); return File(path, "application/x-zip-compressed"); } |
第一個參數指定檔案路徑,第二個參數指定檔案的 MIME 類型。使用者點擊瀏覽器上的下載連結後,會調出下載視窗:
大家應該注意到,檔案名稱會變成 Download1.zip,預設成了 Action 的名字。我們使用 File 方法的第二個重載來解決檔案名稱的問題:
2. 指定 fileDownloadName
public ActionResult FilePathDownload2() { var path = Server.MapPath("~/Files/鶴衝天.zip"); return File("g:\\鶴衝天.zip", "application/x-zip-compressed", "crane.zip"); }public ActionResult FilePathDownload3() { var path = Server.MapPath("~/Files/鶴衝天.zip"); var name = Path.GetFileName(path); return File(path, "application/x-zip-compressed", name); } |
我們可以通過給 fileDownloadName 參數傳值來指定檔案名稱,fileDownloadName 不必和磁碟上的檔案名稱一樣。下載提示視窗分別如下:
FilePathDownload2 沒問題,FilePathDownload3 還是預設為了 Action 的名字。原因是 fileDownloadName 將作為 URL 的一部分,只能包含 ASCII 碼。我們把 FilePathDownload3 改進一下:
3. 對 fileDownloadName 進行 Url 編碼
public ActionResult FilePathDownload4() { var path = Server.MapPath("~/Files/鶴衝天.zip"); var name = Path.GetFileName(path); return File(path, "application/x-zip-compressed", Url.Encode(name)); } |
再試下,下載視窗如下:
好了,沒問題了。上面代碼中 Url.Encode(…),也可使用 HttpUtility.UrlEncode(…),前者在內部調用後者。
我們再來看 FileContentResult.
FileContentResult
FileContentResult 可以直接將 byte[] 以檔案形式發送至瀏覽器(而不用建立臨時檔案)。參考代碼如下:
public ActionResult FileContentDownload1() { byte[] data = Encoding.UTF8.GetBytes("歡迎訪問 鶴衝天 的部落格 http://www.cnblogs.com/ldp615/"); return File(data, "text/plain", "welcome.txt"); } |
點擊後下載連結後,彈出提示視窗如下:
FileStreamResult
想給 FileStreamResult 找一個恰當的例子是不太容易的,畢竟 Http Response 中已經包含了一個輸出資料流,如果要動態組建檔案的話,可以直接向這個輸出資料流中寫入資料,效率還高。當然,我們不會在 Controller 中直接向 Response 的 OutputStream 寫入資料,這樣做是不符合MVC的,我們應該把這個操作封裝成一個 ActionResult。
不過仔細想想,用途還是有的,比如伺服器上有個壓縮(或加密)檔案,需要解壓(或解密)後發送給使用者。
1. 解壓(或解密)
示範代碼如下,解壓使用 ICSharpCode.SharpZipLib.dll:
public ActionResult FileStreamDownload1() { var path = Server.MapPath("~/Files/鶴衝天.zip"); var fileStream = new FileStream(path, FileMode.Open); var zipInputStream = new ZipInputStream(fileStream); var entry = zipInputStream.GetNextEntry(); return File(zipInputStream, "application/pdf", Url.Encode(entry.Name)); } |
簡單起見,假定壓縮檔中只有一個檔案,且是 pdf 格式的。鶴衝天.zip 如下:
點擊後彈出下載提示視窗如下:
2. 轉寄(或盜鏈)
FileStreamResult 的另一種用途是將其它網站上的檔案作為本站檔案下載(其實就是盜鏈):
public ActionResult FileStreamDownload1(){ var stream = new WebClient().OpenRead("http://files.cnblogs.com/ldp615/Mvc_TextBoxFor.rar"); return File(stream, "application/x-zip-compressed", "Mvc_TextBoxFor.rar");} |
看下面提示視窗,來源還是 localhost:
思想火花,照亮世界!