在 Controller 中我們可以使用 FileResult 向客戶端發(fā)送文件。
FileResult
FileResult 是一個(gè)抽象類,繼承自 ActionResult。在 System.Web.Mvc.dll 中,它有如上三個(gè)子類,分別以不同的方式向客戶端發(fā)送文件。
在實(shí)際使用中我們通常不需要直接實(shí)例化一個(gè) FileResult 的子類,因?yàn)?Controller 類已經(jīng)提供了六個(gè) 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 直接將磁盤上的文件發(fā)送至瀏覽器:
1. 最簡單的方式
public ActionResult FilePathDownload1() { var path = Server.MapPath("~/Files/鶴沖天.zip"); return File(path, "application/x-zip-compressed"); }
第一個(gè)參數(shù)指定文件路徑,第二個(gè)參數(shù)指定文件的 MIME 類型。
用戶點(diǎn)擊瀏覽器上的下載鏈接后,會(huì)調(diào)出下載窗口:
大家應(yīng)該注意到,文件名稱會(huì)變成 Download1.zip,默認(rèn)成了 Action 的名字。我們使用 File 方法的第二個(gè)重載來解決文件名的問題:
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 參數(shù)傳值來指定文件名,fileDownloadName 不必和磁盤上的文件名一樣。下載提示窗口分別如下:
FilePathDownload2 沒問題,F(xiàn)ilePathDownload3 還是默認(rèn)為了 Action 的名字。原因是 fileDownloadName 將作為 URL 的一部分,只能包含 ASCII 碼。我們把 FilePathDownload3 改進(jìn)一下:
3. 對 fileDownloadName 進(jìn)行 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(…),前者在內(nèi)部調(diào)用后者。
我們再來看 FileContentResult.
FileContentResult
FileContentResult 可以直接將 byte[] 以文件形式發(fā)送至瀏覽器(而不用創(chuàng)建臨時(shí)文件)。參考代碼如下:
public ActionResult FileContentDownload1() { byte[] data = Encoding.UTF8.GetBytes("歡迎訪問 鶴沖天 的博客 http://www.cnblogs.com/ldp615/"); return File(data, "text/plain", "welcome.txt"); }
點(diǎn)擊后下載鏈接后,彈出提示窗口如下:
FileStreamResult
想給 FileStreamResult 找一個(gè)恰當(dāng)?shù)睦邮遣惶菀椎模吘?Http Response 中已經(jīng)包含了一個(gè)輸出流,如果要?jiǎng)討B(tài)生成文件的話,可以直接向這個(gè)輸出流中寫入數(shù)據(jù),效率還高。當(dāng)然,我們不會(huì)在 Controller 中直接向 Response 的 OutputStream 寫入數(shù)據(jù),這樣做是不符合MVC的,我們應(yīng)該把這個(gè)操作封裝成一個(gè) ActionResult。
不過仔細(xì)想想,用途還是有的,比如服務(wù)器上有個(gè)壓縮(或加密)文件,需要解壓(或解密)后發(fā)送給用戶。
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)); }
簡單起見,假定壓縮文件中只有一個(gè)文件,且是 pdf 格式的。鶴沖天.zip 如下:
點(diǎn)擊后彈出下載提示窗口如下:
2. 轉(zhuǎn)發(fā)(或盜鏈)
FileStreamResult 的另一種用途是將其它網(wǎng)站上的文件作為本站文件下載(其實(shí)就是盜鏈):
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: