ASP.NET 回應大型檔案的注意事項

  分享到噗浪!

當使用 ASP.NET 回應大型檔案的時候,通常有三種方式可以實做,但使用上有幾個地方要特別注意:

第一種:使用 Response.WriteFile() 方法

    Response.ContentType = "application/download";
    Response.WriteFile(@"C:\VeryBigFile.zip");

注意事項:

  1. 當回應過大的檔案時,可能會導致 ASP.NET 的 Worker Proccess 被強制回收,導致下載失敗。參見:PRB: Response.WriteFile cannot download a large file
  2. 使用此方法會支援檔案續傳功能。( HTTP 1.1 )

第二種:使用 Response.BinaryWrite() 方法

 

    Response.ContentType = "application/download";
    Response.BinaryWrite(File.ReadAllBytes(@"C:\VeryBigFile.zip"));

注意事項:

  1. 使用此方法會支援檔案續傳功能。( HTTP 1.1 )

第三種:使用 Response.TransmitFile() 方法

    Response.ContentType = "application/download";
    Response.TransmitFile(@"C:\VeryBigFile.zip");

注意事項:

  1. 當需要回應很大的檔案時,最適合用此方法!因為使用 TransmitFile() 方法不會將檔案內容緩存於記憶體中,執行的效能最好。
  2. 缺點是使用此方法會讓 ASP.NET 無法支援續傳功能。

相關網址

 

此文章由 will 發表於 2008/3/12 上午 12:09:00

永久連結 | 評論 (2) | 此文章的RSSRSS comment feed |

分類: .Net | ASP.NET | C#

標籤: ,

評論

四月 6. 2010 22:32

Will 保哥

剛看到另一個下載大檔案的方式,可提供更仔細的檔案下載控制。

[ASP.NET]回應大型檔案下載
www.dotblogs.com.tw/.../14102.aspx

Will 保哥 Taiwan

十月 13. 2011 19:26

Victor Tseng

請問使用BinaryWrite()的話,是表示不用自己去處理相關細節 (Range 啊 ETag 等東西) 的意思嗎?

Victor Tseng Taiwan

新增評論


( 您輸入的Email不會顯示於網站上 )

  Country flag

biuquote
  • 評論
  • 線上預覽
Loading