本文介绍了FileStreamResult是否关闭Stream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的问题类似于以下问题: File()是否在asp.net mvc中关闭流?
My question is similar to this question:Does File() In asp.net mvc close the stream?
在C#MVC 4中,我有以下内容.
I have the follows in C# MVC 4.
FileStream fs = new FileStream(pathToFileOnDisk, FileMode.Open);
FileStreamResult fsResult = new FileStreamResult(fs, "Text");
return fsResult;
fs
是否会被FileStreamResult
自动关闭?谢谢!
Will fs
be closed automatically by FileStreamResult
? thanks!
推荐答案
是.它在流周围使用using
块,并确保资源将被处置.
Yes. It uses a using
block around the stream, and that ensures that the resource will dispose.
这是FileStreamResult
WriteFile方法的内部实现:
Here is the internal implementation of the FileStreamResult
WriteFile method:
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);
}
}
}
这篇关于FileStreamResult是否关闭Stream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!