My ActionResult
returns a File
but I also need it to conditionally return a Stream
.
I have not been able to find documentation on how an ActionResult
can return a Stream
.
Here is my code for return of a file:
return File(memoryStream,.... )
As mentioned, I need to return just a Stream
.
To return a file stream, you can use a FileStreamResult. This lets you specify the stream, the MIME type, and some other options (like the file name). // the stream when the transfer is complete.
Yes. It uses a using block around the stream, and that ensures that the resource will dispose.
my previous answer was dated.
as of now, the File
returns different type of ActionResult depends on given arguments
// to return FileStreamResult return File(memoryStream, "application/pdf"); // or.. return File(memoryStream, "application/pdf", "file_name");
Use FileStreamResult
:
MemoryStream stream = someService.GetStream(); return new FileStreamResult(stream, "application/pdf")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With