標籤: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 向 瀏覽器 傳送檔案