Convert string file name to FormFile in .NET 6

75 views Asked by At

I'm designing a web app using .net6 that contains a model and a view model like this:

public class Course
{
        public string CourseImageName { get; set; }
        public string DemoFileName { get; set; }
}

public class EditCourseViewModel
{
       public IFormFile CourseImageName { get; set; }
       public IFormFile DemoFileName { get; set; }
}

Create course is done successfully but in case of edit course when preview data, CourseImageName and DemoFileName in the model class that are string, should convert to IFormFile to show and then in case of any change of images save in database. how string file name can be convert to IFormFile for show as preview?

I need to find a way to convert string file name to IFormFile.

1

There are 1 answers

0
Guru Stron On

IFormFile as docs claim:

Represents a file sent with the HttpRequest.

And should not be used in the model to get data.

how string file name can be convert to IFormFile for show as preview?

You should use the filename to build URL for the image and return it to the frontend.

Based on how you store the image on server you can either setup the middleware to serve static files or create your own action which will return the file (for example via ControllerBase.File method).