ASP.NET MVC:通過 FileResult 向 瀏覽器 發送文件


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.Encode
public 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 向 瀏覽器 發送文件


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM