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!

Ivan
  • 49
  • 2
  • 7
  • Are you going to store the file in SQL Server FileStream? – William Xifaras May 20 '19 at 19:50
  • Not sure what is best option. Is it better like that? It's up to me how to store it. – Ivan May 20 '19 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? – William Xifaras May 20 '19 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 – Ivan May 20 '19 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. – William Xifaras May 20 '19 at 20:25
  • https://learn.microsoft.com/en-us/sql/relational-databases/blob/filestream-sql-server?view=sql-server-2017 – William Xifaras May 20 '19 at 20:27
  • @Ivan this can be of help too https://stackoverflow.com/questions/35379309/how-to-upload-files-in-asp-net-core – David Zagi May 20 '19 at 20:43
  • [This repo in GitHub](https://github.com/LazZiya/FileUpload) shows how to use drag & drop upload, delete files and resize images – LazZiya May 21 '19 at 04:31

1 Answers1

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();
        }
    }
}
Xueli Chen
  • 11,987
  • 3
  • 25
  • 36
  • Thank you a lot, this is what I was looking for – Ivan May 28 '19 at 11:14
  • @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 ! – Xueli Chen May 29 '19 at 01:21
  • @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? – Robert Achmann Feb 19 '21 at 16:56