Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between FileContentResult and FileStreamResult

I'm editing some code and there is one method which return FileContentResult type. I get a stream from service, so for me it would be more convenient to change returning type to FileStreamResult.

Should I convert stream to an array to return FileContentResult?

Or can I just change returning type safely?

like image 242
user3818229 Avatar asked Dec 28 '15 17:12

user3818229


People also ask

What is FileContentResult?

FileContentResult. Represents an ActionResult that when executed will write a binary file to the response. FileStreamResult. Represents an ActionResult that when executed will write a file from a stream 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.

How do I return FileStreamResult?

You can do this simply using the Controller. File method which returns a FilePathResult. There are a number of overloads, but here is an example: return File(streamreader.

Which of the following classes returns the content of a binary file?

FileContentResult: Sends the contents of a binary file to the response.


2 Answers

Both FileStreamResult and FileContentResult inherit from FileResult which inherits from ActionResult. So you can return either one type from a method which has ActionResult as the return type

If you already have a stream, you can use FileStreamResult constructor to return a FileResult

public ActionResult Download()
{
    var f = Server.MapPath("~/Content/mypdf.pdf");
    var fileStream = new FileStream(f,FileMode.Open,FileAccess.Read);
    return new FileStreamResult(fileStream, MimeMapping.GetMimeMapping(f));
}

If you already have a byte arrray, you can use FileContentResult constructor to return a FileResult

public ActionResult Download()
{
    var f = Server.MapPath("~/Content/mypdf.pdf");
    var bytes = System.IO.File.ReadAllBytes(f);
    return new FileContentResult(bytes, MimeMapping.GetMimeMapping(f));
}

The Controller.File method has overloads which takes either a byte array or a stream

public ActionResult Download()
{
    var f = Server.MapPath("~/Content/mypdf.pdf");
    var bytes = System.IO.File.ReadAllBytes(f);
    return File(bytes, MimeMapping.GetMimeMapping(f));
}
public ActionResult Download2()
{
    var f = Server.MapPath("~/Content/mypdf.pdf");
    var fileStream = new FileStream(f, FileMode.Open, FileAccess.Read);
    return File(fileStream, MimeMapping.GetMimeMapping(f));
}

If the browser has the support to display the content type of the response, the response will be displayed in the browser. For example, for the above code, it will display the pdf content in the browser.

There is another overload of the File method which takes the download file name which the browsers' save/download dialog will use so that user can save it his local computer and/or open.

public ActionResult Download4()
{
    var f = Server.MapPath("~/Content/mypdf.pdf");
    var fileStream = new FileStream(f, FileMode.Open, FileAccess.Read);
    return File(fileStream, MimeMapping.GetMimeMapping(f),"MyfileNiceFileName.pdf");
}

With this, user will get a download prompt from the browser.

like image 55
Shyju Avatar answered Oct 19 '22 02:10

Shyju


FileResult is an abstract base class for all the others.

  • FileContentResult - use it when you have a byte array you would like to return as a file
  • FileStreamResult - you have a stream open, you want to return it's content as a file

Reference: What's the difference between the four File Results in ASP.NET MVC

like image 41
casillas Avatar answered Oct 19 '22 02:10

casillas