---
# System prepended metadata

title: HttpResponse輸出大檔案

---

# HttpResponse輸出大檔案
透過Web Server給使用者下載檔案，會用以下方式，  
但遇到檔案size大於2^31-1(int32的最大值)時會發生溢位錯誤。
``` CSharp
Response.WriteFile(fileNamePath);
```

改用以下方式，讀取檔案binary資料，再輸出，  
且停用緩存，`Response.Buffer = false`。

``` CSharp
var outStream = context.Response.OutputStream;

using (var inStream = System.IO.File.OpenRead(fileNamePath))
{
    var buffer = new byte[2048];
    int len = inStream.Read(buffer, 0, buffer.Length);

    // 停用緩存
    context.Response.Buffer = false;

    while (len > 0)
    {
        outStream.Write(buffer, 0, len);
        len = inStream.Read(buffer, 0, buffer.Length);
    }
    
    // 確認Response Stream寫出
    outStream.Flush();
}
// 確認Response寫出
context.Response.Flush();
```