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?
Both
FileStreamResult
andFileContentResult
inherit fromFileResult
which inherits fromActionResult
. So you can return either one type from a method which hasActionResult
as the return typeIf you already have a stream, you can use
FileStreamResult
constructor to return aFileResult
If you already have a byte arrray, you can use
FileContentResult
constructor to return aFileResult
The
Controller.File
method has overloads which takes either a byte array or a streamIf 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.With this, user will get a download prompt from the browser.
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 fileFileStreamResult
- you have a stream open, you want to return it's content as a file