西西軟件園多重安全檢測(cè)下載網(wǎng)站、值得信賴的軟件下載站!
軟件
軟件
文章
搜索

首頁(yè)編程開(kāi)發(fā)ASP.NET → Asp.net中通過(guò) FileResult 向?yàn)g覽器發(fā)送文件

Asp.net中通過(guò) FileResult 向?yàn)g覽器發(fā)送文件

相關(guān)軟件相關(guān)文章發(fā)表評(píng)論 來(lái)源:本站整理時(shí)間:2010/9/17 20:54:31字體大。A-A+

作者:佚名點(diǎn)擊:84次評(píng)論:0次標(biāo)簽: 瀏覽器

07073游戲?yàn)g覽器v2.0.1.6 官方正式版
  • 類型:瀏覽器類大。3.7M語(yǔ)言:中文 評(píng)分:6.4
  • 標(biāo)簽:
立即下載

在 Controller 中我們可以使用 FileResult 向客戶端發(fā)送文件。

FileResult
 

FileResult

FileResult 是一個(gè)抽象類,繼承自 ActionResult。在 System.Web.Mvc.dll 中,它有如上三個(gè)子類,分別以不同的方式向客戶端發(fā)送文件。

在實(shí)際使用中我們通常不需要直接實(shí)例化一個(gè) FileResult 的子類,因?yàn)?Controller 類已經(jīng)提供了六個(gè) File 方法來(lái)簡(jiǎn)化我們的操作:

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. 最簡(jiǎn)單的方式

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)出下載窗口:

FilePathDownload1

大家應(yīng)該注意到,文件名稱會(huì)變成 Download1.zip,默認(rèn)成了 Action 的名字。我們使用 File 方法的第二個(gè)重載來(lái)解決文件名的問(wèn)題:

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);
}

我們可以通過(guò)給 fileDownloadName 參數(shù)傳值來(lái)指定文件名,fileDownloadName 不必和磁盤上的文件名一樣。下載提示窗口分別如下:

FilePathDownload2

FilePathDownload3

FilePathDownload2 沒(méi)問(wèn)題,F(xiàn)ilePathDownload3 還是默認(rèn)為了 Action 的名字。原因是 fileDownloadName 將作為 URL 的一部分,只能包含 ASCII 碼。我們把 FilePathDownload3 改進(jìn)一下:

3. 對(duì) 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));
}

再試下,下載窗口如下:

FilePathDownload4

好了,沒(méi)問(wèn)題了。上面代碼中 Url.Encode(…),也可使用 HttpUtility.UrlEncode(…),前者在內(nèi)部調(diào)用后者。

我們?cè)賮?lái)看 FileContentResult.

FileContentResult
 

FileContentResult 可以直接將 byte[] 以文件形式發(fā)送至瀏覽器(而不用創(chuàng)建臨時(shí)文件)。參考代碼如下:

public ActionResult FileContentDownload1()
{
    byte[] data = Encoding.UTF8.GetBytes("歡迎訪問(wèn) 鶴沖天 的博客 http://www.cnblogs.com/ldp615/");
    return File(data, "text/plain", "welcome.txt");
}

點(diǎn)擊后下載鏈接后,彈出提示窗口如下:

FileContentDownload1

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。

不過(guò)仔細(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));
}

簡(jiǎn)單起見(jiàn),假定壓縮文件中只有一個(gè)文件,且是 pdf 格式的。鶴沖天.zip 如下:

FileStreamDownload1

點(diǎn)擊后彈出下載提示窗口如下:

FileStreamDownload1-2

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");
}

看下面提示窗口,來(lái)源還是 localhost:

FileStreamDownload2

    游戲?yàn)g覽器
    (36)游戲?yàn)g覽器
    西西軟件園提供最好用的游戲?yàn)g覽器,游戲?yàn)g覽器是專門針對(duì)網(wǎng)頁(yè)游戲而設(shè)計(jì)的專用瀏覽器,游戲?yàn)g覽器能夠給玩家提供加速功能,還有各種輔助工具,最終要是支持快捷按鍵.網(wǎng)絡(luò)上有很多游戲?yàn)g覽器,到底哪個(gè)游戲?yàn)g覽器最好用呢西西小編就整理出了最好用的瀏覽器排行榜給大家選擇....更多>>

    相關(guān)評(píng)論

    閱讀本文后您有什么感想? 已有人給出評(píng)價(jià)!

    • 8 喜歡喜歡
    • 3 頂
    • 1 難過(guò)難過(guò)
    • 5 囧
    • 3 圍觀圍觀
    • 2 無(wú)聊無(wú)聊

    熱門評(píng)論

    最新評(píng)論

    發(fā)表評(píng)論 查看所有評(píng)論(0)

    昵稱:
    表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
    字?jǐn)?shù): 0/500 (您的評(píng)論需要經(jīng)過(guò)審核才能顯示)