Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does File() In asp.net mvc close the stream?

Tags:

c#

asp.net-mvc

I am wondering if you do something like

public FileResult result()
{
   Stream stream = new Stream();
   return File(stream,"text/html","bob.html");
}

if File() would close the stream for you? Since I tried to put the "stream" in a using statement but it always gave me a error saying the stream was closed.

public FileResult result()
{
    using(Stream stream = new Stream())
    {
       return File(stream,"text/html","bob.html");
    }
}
like image 809
chobo2 Avatar asked Jan 24 '10 23:01

chobo2


People also ask

What is file Result in MVC?

FileContentResult Class (Microsoft. AspNetCore. Mvc) Represents an ActionResult that when executed will write a binary file to the response.

Does FileStreamResult dispose stream?

ASP.NET will handle getting rid of this stream for you once the transfer is complete (part of the FileStreamResult), so you don't need to worry about cleaning it up.


1 Answers

If you are using the File object to send the resulting file for download as bob.html then yes.

I believe that all standard Streams (OutputStream, FileStream, CryptoStream) will attempt to flush when closed or disposed.

There are a number of classes within the MVC framework that implement the base FileResult class.

System.Web.Mvc.FileResult
System.Web.Mvc.FileContentResult
System.Web.Mvc.FilePathResult
System.Web.Mvc.FileStreamResult
like image 58
Kane Avatar answered Sep 26 '22 17:09

Kane