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!
-
Are you going to store the file in SQL Server FileStream?William Xifaras– William Xifaras2019-05-20 19:50:18 +00:00Commented 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.Ivan– Ivan2019-05-20 20:00:50 +00:00Commented 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?William Xifaras– William Xifaras2019-05-20 20:09:20 +00:00Commented 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 correctlyIvan– Ivan2019-05-20 20:24:32 +00:00Commented 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.William Xifaras– William Xifaras2019-05-20 20:25:42 +00:00Commented May 20, 2019 at 20:25
|
Show 3 more comments
1 Answer
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();
}
}
}
3 Comments
Ivan
Thank you a lot, this is what I was looking for
Xueli Chen
@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 !
Robert Achmann
@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?