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

來源:互聯網
上載者:User

標籤:use   other   protected   例子   封裝   when   需要   net   entry   

FileResult is an abstract base class for all the others.

  • FileContentResult - you use it when you have a byte array you would like to return as a file
  • FilePathResult - when you have a file on disk and would like to return it‘s content (you give a path)
  • FileStreamResult - you have a stream open, you want to return it‘s content as a file

However, you‘ll rarely have to use these classes - you can just use one of Controller.Fileoverloads and let asp.net mvc do the magic for you.

 

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
public ActionResult FilePathDownload1(){    var path = Server.MapPath("~/Files/BarcodeConverter.exe");    return File(path, "application/x-zip-compressed");}public ActionResult FilePathDownload2(){    var path = Server.MapPath("~/Files/BarcodeConverter.exe");   return File("g:\\BarcodeConverter.exe", "application/x-zip-compressed", "BarcodeConverter.exe"); } public ActionResult FilePathDownload3(){    var path = Server.MapPath("~/Files/BarcodeConverter.exe");    var name = Path.GetFileName(path);     return File(path, "application/x-zip-compressed", name); }//FilePathDownload3  下載後的檔案名稱還是預設為了 Action 的名字。原因是 fileDownloadName 將作為 URL 的一部分,只能包含 ASCII 碼。所以,我們需要對name進行encode Url.Encodepublic ActionResult FilePathDownload4() {     var path = Server.MapPath("~/Files/BarcodeConverter.exe");     var name = Path.GetFileName(path);     return File(path, "application/x-zip-compressed",Url.Encode(name)); }

 

FileContentResult

FileContentResult 可以直接將 byte[] 以檔案形式發送至瀏覽器(而不用建立臨時檔案)

public FileResult Download(){    byte[] fileBytes = System.IO.File.ReadAllBytes(@"c:\folder\myfile.txt");    string fileName = "myfile.txt";    return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);}
FileStreamResult

想給 FileStreamResult 找一個恰當的例子是不太容易的,畢竟 Http Response 中已經包含了一個OutputStream屬性,

如果要動態組建檔案的話,可以直接向這個輸出資料流中寫入資料,效率還高。

當然,我們不會在 Controller 中直接向 Response 的 OutputStream 寫入資料,這樣做是不符合MVC的,我們應該把這個操作封裝成一個 ActionResult。

不過仔細想想,用途還是有的,比如伺服器上有個壓縮(或加密)檔案,需要解壓(或解密)後發送給使用者,或者轉寄(或盜鏈)

 (1)解壓(或解密)
public ActionResult FileStreamDownload1(){    var path = Server.MapPath("~/Files/myfile.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 格式的。}
(2)轉寄(或盜鏈)
將其它網站上的檔案作為本站檔案下載(其實就是盜鏈):
public ActionResult FileStreamDownload1(){    var stream = new WebClient().OpenRead("http://files.cnblogs.com/level/test.rar");    return File(stream, "application/x-zip-compressed", "test.rar"); }

 

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

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

聯繫我們

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