Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert FileStreamResult to IFormFile?

Tags:

c#

asp.net-mvc

I change the size of the image with this code. But this method returns FileStreamResult. I want to convert FileStreamResult to IFromFile. How can I do that?

Note: I am using CoreCompat to change the image size.

public FileStreamResult ChangeSize(IFormFile file)  
{
    using (var img = System.Drawing.Image.FromStream(file.OpenReadStream()))
    {
        Stream ms = new MemoryStream(img.Resize(100, 100).ToByteArray());

        return new FileStreamResult(ms, "image/jpg");
    }
}
like image 262
Fatikhan Gasimov Avatar asked May 24 '17 17:05

Fatikhan Gasimov


People also ask

What is IFormFile C#?

What is IFormFile. ASP.NET Core has introduced an IFormFile interface that represents transmitted files in an HTTP request. The interface gives us access to metadata like ContentDisposition, ContentType, Length, FileName, and more. IFormFile also provides some methods used to store files.

What is FileStreamResult C#?

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.


1 Answers

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);
    }
}
like image 186
Captain Prinny Avatar answered Sep 24 '22 12:09

Captain Prinny