首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> 關於ASP.NET

ASP.NET MVC ActionResult 本質解析:FileResult 檔案回應三大實作詳解

     編輯:關於ASP.NET

FileResult 抽象基類

public abstract class FileResult : ActionResult
{
    protected FileResult(string contentType);
    public override void ExecuteResult(ControllerContext context);
    protected abstract void WriteFile(HttpResponseBase response);
    
    public string ContentType { get; }
    public string FileDownloadName { get; set; } // 設定則啟用附件模式
}

ExecuteResult 共通邏輯

  1. 設定 Response.ContentType。
  2. 若 FileDownloadName 非空,產生 Content-Disposition: attachment; filename=... 標頭 (透過 ContentDispositionUtil)。
  3. 呼叫 WriteFile(response) 寫入檔案內容。

三大具體實作

1. FileContentResult:位元組陣列內容

public class FileContentResult : FileResult
{
    public byte[] FileContents { get; }
    public FileContentResult(byte[] fileContents, string contentType);
    
    protected override void WriteFile(HttpResponseBase response)
        => response.OutputStream.Write(FileContents, 0, FileContents.Length);
}

// Controller 方法
protected FileContentResult File(byte[] fileContents, string contentType);
protected virtual FileContentResult File(byte[] fileContents, string contentType, string fileDownloadName);

適用:動態產生內容 (報表、圖表、加密檔案) 或小檔案讀入記憶體。

2. FilePathResult:實體檔案路徑

public class FilePathResult : FileResult
{
    public string FileName { get; }
    public FilePathResult(string fileName, string contentType);
    
    protected override void WriteFile(HttpResponseBase response)
        => response.TransmitFile(FileName); // 高效傳輸,支援大檔案、斷點續傳
}

// Controller 方法
protected FilePathResult File(string fileName, string contentType);
protected virtual FilePathResult File(string fileName, string contentType, string fileDownloadName);

適用:伺服器現有靜態檔案 (PDF、影片、大型下載)。TransmitFile 直接由核心模式傳輸,不佔用 Managed 記憶體。

3. FileStreamResult:資料流內容

public class FileStreamResult : FileResult
{
    public Stream FileStream { get; }
    public FileStreamResult(Stream fileStream, string contentType);
    
    protected override void WriteFile(HttpResponseBase response)
    {
        var buffer = new byte[4096];
        int count;
        while ((count = FileStream.Read(buffer, 0, buffer.Length)) > 0)
            response.OutputStream.Write(buffer, 0, count);
    }
}

// Controller 方法
protected FileStreamResult File(Stream fileStream, string contentType);
protected virtual FileStreamResult File(Stream fileStream, string contentType, string fileDownloadName);

適用:動態串流來源 (資料庫 BLOB、雲端儲存、壓縮串流、加密串流)。需注意 FileStream.Position 及生命週期管理。

媒體類型 (Content-Type) 常見對照

副檔名Content-Type
.pdfapplication/pdf
.jpg/.jpegimage/jpeg
.pngimage/png
.zipapplication/zip
.docxapplication/vnd.openxmlformats-officedocument.wordprocessingml.document

內聯 vs 附件模式

  • 內聯 (預設):瀏覽器嘗試直接顯示 (圖片、PDF)。
  • 附件:設定 FileDownloadName,瀏覽器彈出下載對話框。

總結

FileResult 三兄弟涵蓋所有檔案回應情境:FileContentResult (記憶體陣列)、FilePathResult (實體檔案、高效)、FileStreamResult (串流來源)。選擇原則:靜態大檔用 FilePathResult,動態串流用 FileStreamResult,小型動態內容用 FileContentResult。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved