在 ASP.NET 中实现文件下载时如何处理我的文件流? [英] How do I dispose my filestream when implementing a file download in ASP.NET?
问题描述
我有一个 DocumentGenerator
类,它包装了一个 MemoryStream
.所以我在类上实现了 IDisposable
.
我不知道如何/在哪里处理它.
这是我当前的代码,它在 MVC 中执行文件下载:
using (DocumentGenerator dg = DocumentGenerator.OpenTemplate(path)){/* 一些文档操作DocumentGenerator 在这里...*/返回文件(dg.GetDocumentStream(),文本/纯文本",文件名);}
此错误是因为在控制器完成之前关闭/处置了流.在这种情况下,我如何确保我的资源得到妥善处置?
我目前对 IDisposable
的实现只是处理了 MemoryStream
.我知道这不是一个正确的实现,我只是将它用作测试.我可以在这里做些不同的事情来使其发挥作用吗?
public void Dispose(){_ms.Dispose();_ms = 空;}
您不需要处理流.它将由 FileStreamResult.WriteFile处理一>方法.本课程的代码摘录:
public FileStreamResult(Stream fileStream, string contentType) : base(contentType){如果(文件流 == 空){throw new ArgumentNullException("fileStream");}this.FileStream = fileStream;}protected override void WriteFile(HttpResponseBase response){流 outputStream = response.OutputStream;使用 (this.FileStream){字节[]缓冲区=新字节[0x1000];而(真){int count = this.FileStream.Read(buffer, 0, 0x1000);如果(计数== 0){返回;}outputStream.Write(buffer, 0, count);}}}
注意using
.当您从控制器调用 File(dg.GetDocumentStream(), "text/plain", filename)
时,这会调用构造函数,该构造函数将流存储到在呈现期间处置的公共属性中.>
结论:您无需担心使用 dg.GetDocumentStream()
处理获取的流.
I have a class DocumentGenerator
which wraps a MemoryStream
. So I have implemented IDisposable
on the class.
I can't see how/where I can possibly dispose it though.
This is my current code, which performs a file download in MVC:
using (DocumentGenerator dg = DocumentGenerator.OpenTemplate(path))
{
/* some document manipulation with the
DocumentGenerator goes here ...*/
return File(dg.GetDocumentStream(), "text/plain", filename);
}
This errors as the stream is closed/disposed before the controller has finished with it. How can I make sure my resources are properly disposed in this situation?
EDIT: My implementation of IDisposable
at the moment just disposes the MemoryStream
. I know it's not a proper implementation, I just used it as a test. Is there something different I could do here to make it work?
public void Dispose()
{
_ms.Dispose();
_ms = null;
}
You don't need to dispose the stream. It will be disposed by the FileStreamResult.WriteFile method. Code excerpt from this class:
public FileStreamResult(Stream fileStream, string contentType) : base(contentType)
{
if (fileStream == null)
{
throw new ArgumentNullException("fileStream");
}
this.FileStream = fileStream;
}
protected override void WriteFile(HttpResponseBase response)
{
Stream outputStream = response.OutputStream;
using (this.FileStream)
{
byte[] buffer = new byte[0x1000];
while (true)
{
int count = this.FileStream.Read(buffer, 0, 0x1000);
if (count == 0)
{
return;
}
outputStream.Write(buffer, 0, count);
}
}
}
Notice the using
. When you call File(dg.GetDocumentStream(), "text/plain", filename)
from your controller this invokes the constructor which stores the stream into a public property which is disposed during the rendering.
Conclusion: you don't need to worry about disposing the stream obtain with dg.GetDocumentStream()
.
这篇关于在 ASP.NET 中实现文件下载时如何处理我的文件流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!