我有一個類DocumentGenerator
,它包裝MemoryStream
。所以我已經在班上實施了IDisposable
。如何在ASP.NET中實現文件下載時處理文件流?
我看不到我可以如何處置它。
這是我的當前代碼,執行在MVC文件下載:
using (DocumentGenerator dg = DocumentGenerator.OpenTemplate(path))
{
/* some document manipulation with the
DocumentGenerator goes here ...*/
return File(dg.GetDocumentStream(), "text/plain", filename);
}
該錯誤作爲前控制器已經完成了它的流被關閉/佈置。我怎樣才能確保我的資源在這種情況下妥善處置?
編輯:我的執行IDisposable
此刻剛剛配置了MemoryStream
。我知道這不是一個合適的實現,我只是用它作爲測試。有什麼不同,我可以在這裏做,使其工作?
public void Dispose()
{
_ms.Dispose();
_ms = null;
}
你能告訴我們IDisposable的實現嗎? – DHN 2010-06-21 11:58:53
是否'_ms'與調用'GetDocumentStream'後相同? – 2010-06-21 12:21:30
@Jordão:是的,這是正確的,因此問題。 – fearofawhackplanet 2010-06-21 15:05:13