Search code examples
c#asp.netfilecontentresultfilestreamresult

How do I convert a FileContentResult to a FileStreamResult?


I have a FileContentResult obtained from a byte array and want to turn it into a FileStreamResult. Is this possible? If so, how?


Solution

  • Firstly, I don't know much about MVC but it sounds like you shouldn't need to do this. If you have code which depends on FileStreamResult, see if you could make it depend on FileResult instead, which is the base class of both FileStreamResult and FileContentResult.

    But otherwise, you could always use:

    var streamResult = new FileStreamResult(new MemoryStream(contentResult.FileContents),
                                            contentResult.ContentType)
    streamResult.FileDownloadName = contentResult.FileDownloadName;
    // You could use an object initializer instead of a separate statement, of course.