如何实施ASP.NET文件下载时,我处理我的文件流? [英] How do I dispose my filestream when implementing a file download in ASP.NET?
问题描述
我有一个类 DocumentGenerator
它包装一个的MemoryStream
。所以,我已实施的IDisposable
的类。
我看不出如何/在哪里我都不可能处理它,虽然。
这是我目前的code,它执行的MVC文件下载:
使用(DocumentGenerator DG = DocumentGenerator.OpenTemplate(路径))
{
/ *一些文档操作与
DocumentGenerator在这儿... ... * / 返回文件(dg.GetDocumentStream(),text / plain的文件名);
}
这个错误,因为流之前控制器已经完成了它被关闭/处置。我怎样才能确保我的资源,妥善处理这种情况?
编辑:我此刻的IDisposable
的实施只是处置的的MemoryStream
。我知道这是不是一个正确的实施,我只是用它作为一个测试。是否有不同的东西,我可以在这里做的,使其工作?
公共无效的Dispose()
{
_ms.Dispose();
_MS = NULL;
}
您不需要处理的数据流。它将由<一个被布置href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.filestreamresult.writefile.aspx\">FileStreamResult.WriteFile方法。从这个类code节选:
公共FileStreamResult(流FILESTREAM,字符串的contentType):基地(的contentType)
{
如果(FILESTREAM == NULL)
{
抛出新的ArgumentNullException(FILESTREAM);
}
this.FileStream = FILESTREAM;
}保护覆盖无效WriteFile的(HTT presponseBase响应)
{
流的OutputStream = response.OutputStream;
使用(this.FileStream)
{
字节[]缓冲区=新的字节[为0x1000];
而(真)
{
诠释计数= this.FileStream.Read(缓冲,0,0×1000);
如果(计数== 0)
{
返回;
}
outputStream.Write(缓冲,0,计数);
}
}
}
注意使用
。当你调用文件(dg.GetDocumentStream(),text / plain的文件名)
从您的控制器此调用该流存储到其设置的公共属性的构造在渲染过程中。
结论:你不必担心处理流与 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屋!