How to convert FileStreamResult to IFormFile?

This should handle converting your FileStreamResult to a FormFile:

public IFormFile ReturnFormFile(FileStreamResult result)
{
    var ms = new MemoryStream();
    try
    {
        result.FileStream.CopyTo(ms);
        return new FormFile(ms, 0, ms.Length);
    }
    catch(Exception e){
       ms.Dispose();
       throw;
    }
    finally
    {
        ms.Dispose();
    }
}

This works for the file but it doesn't appear to copy other data, such as the content type

public IFormFile GetFormFile(FileStreamResult fsr)
{
    using (var fs = fsr.FileStream)
    {
        file = new FormFile(fs, 0, fs.Length, "name", fsr.FileDownloadName);
    }
}

Tags:

C#

Asp.Net Mvc