How to write saved file id and name and URL in the Model

SaeedP

Active member
Joined
Oct 21, 2020
Messages
34
Programming Experience
3-5
Hello,

This code saves files on wwwroot folder:

C#:
[Route("WeatherForecast/Post")]
        [HttpPost]
        public IActionResult Post([FromForm] FileModel file)
        {
            try
            {
                string path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", file.FileName);

                using (Stream stream = new FileStream(path, FileMode.Create))
                {

                    file.FormFile.CopyTo(stream);

                }

                return StatusCode(StatusCodes.Status201Created);

            }

            catch (Exception)
            {
                return StatusCode(StatusCodes.Status500InternalServerError);
            }
        }

How to save Id and name and URL in the model? is it possible for a controller to do these tasks?

regards,
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,583
Location
Chesapeake, VA
Programming Experience
10+
Yes, you would just code to add also save the Id, name, and URL in the controller to do that. Exactly how you write that code depends on where you are planning of persisting the data to.

If this FileModel is the same one as from your other thread, have you figured out how to get EF to create the database? Once the database is created, you can call the appropriate EF methods to perform the save.
 
Top Bottom