tot*_*oro 26 c# asp.net asp.net-mvc
我的问题类似于这个问题: 文件()在asp.net mvc中关闭流吗?
我在C#MVC 4中有以下内容.
FileStream fs = new FileStream(pathToFileOnDisk, FileMode.Open);
FileStreamResult fsResult = new FileStreamResult(fs, "Text");
return fsResult;
Run Code Online (Sandbox Code Playgroud)
会fs自动关闭FileStreamResult吗?谢谢!
Far*_*ina 34
Yes. It uses a using block around the stream, and that ensures that the resource will dispose.
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)