在File
方法返回一個ActionResult
的情況下,你是轉移責任流關閉的動作,結果,所以你的確不想打電話Dispose
或使用using
。這是爲了使ActionResult
不需要緩衝數據。所以:剛取出using
:
public ActionResult Download(string filename, string credentials)
{
....
var res = /* response from s3 */
return File(res, type, filename);
}
如果你有不平凡的代碼,你可以把它更加複雜:
public ActionResult Download(string filename, string credentials)
{
....
Stream disposeMe = null;
try {
// ...
disposeMe = /* response from s3 */
// ...
var result = File(disposeMe, type, filename);
disposeMe = null; // successfully created File result which
// now owns the stream, so *leave stream open*
return result;
} finally {
disposeMe?.Dispose(); // if not handed over, dispose
}
}
什麼是'從s3'迴應? – DavidG
大小爲50 MB的Zip文件 – anand
不,不,確切的方法/函數/對象/數據類型/什麼? – DavidG