ASP.NET MVC:通過FileResult向瀏覽器傳送檔案

來源:互聯網
上載者:User

在 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:

思想火花,照亮世界!

相關文章

聯繫我們

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