1

My task is to create a model and database for web API and CRUD purposes, one of the model properties is the photo of a car. While hardcoding data for database migration, how to set that property as a photo and save a photo path to SQL database. Later I have to manipulate with the Postman to make CRUD and API manipulations with that photo and also the other properties of that car. What is the easiest solution? I have found some info about IFormFile and byte but not sure how to do that correctly. I am using asp.net core 2.2. Thank you!

8
  • Are you going to store the file in SQL Server FileStream? Commented May 20, 2019 at 19:50
  • Not sure what is best option. Is it better like that? It's up to me how to store it. Commented May 20, 2019 at 20:00
  • FileStream enables SQL Server-based applications to store unstructured data, such as documents and images, on the file system. Is it your best option? Opinions will vary, but its a good option if you are using SQL Server. But it only applies to SQL Server (Windows only). Are you using Azure? Commented May 20, 2019 at 20:09
  • I understand, no not using azure. I read a lot about file stream and I know what is the usage of it but, don't know to apply it correctly Commented May 20, 2019 at 20:24
  • You will have to read the docs on it. It's not hard to set up. If you plan on using it, going from IFormFile to FileStream is simple. Commented May 20, 2019 at 20:25

1 Answer 1

6

You could try to follow steps below :

1.Add a new folder to the project and call it wwwroot , and create images folder and Cars subfolder in wwwroot folder.

2.Model

public class Car
{
    public int Id { get; set; }
    public string CarName { get; set; }
    public string ImagePath { get; set; }
}
public class CarViewModel
{
    public string CarName { get; set; }
    public IFormFile Image { get; set; }
}

3.Controller

 [Route("api/[controller]")]
[ApiController]
public class CarsController : ControllerBase
{
    private readonly IWebHostEnvironment _hostingEnv;
    private readonly WebAPIDbContext _context;
    
    public CarsController(WebAPIDbContext context, IWebHostEnvironment hostingEnv)
    {
        _hostingEnv = hostingEnv;
        _context = context;
    }

    [HttpPost]
    public async Task<ActionResult> Post([FromForm] CarViewModel carVM)
    {
        if (carVM.Image != null)
        {
            var a = _hostingEnv.WebRootPath;
            var fileName = Path.GetFileName(carVM.Image.FileName);
            var filePath = Path.Combine(_hostingEnv.WebRootPath, "images\\Cars", fileName);

            using (var fileSteam = new FileStream(filePath, FileMode.Create))
            {
                await carVM.Image.CopyToAsync(fileSteam);
            }
            
            Car car = new Car();
            car.CarName = carVM.CarName;
            car.ImagePath = filePath;  //save the filePath to database ImagePath field.
            _context.Add(car);
            await _context.SaveChangesAsync();
            return Ok();
        }
        else
        {
            return BadRequest();
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you a lot, this is what I was looking for
@Ivan , I am glad to help you resolve the issue . Could you mark my reply as an answer? This will help other people who have the same doubts to find the answer quicly .Thanks a lot !
@XueliChen IHostingEnvironment is marked deprecated in Core 5, and that we're supposed to use IWebHostingEnvironment - can you update your solution to show it's usage without getting a CS0051 error?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.