After creating a POST
method for my API that allows me to upload images to the database where I hold them as bytes, I wanted to create a GET
method that will allow me to get their information and eventually show them on a web page.
My model class looks like this:
public class Image
{
public int recipeId { get; set; }
public string format { get; set; }
public string description { get; set; }
public IFormFile image { get; set; }
}
IFormFile image
is the image that gets uploaded from the front-end and gets converted using MemoryStream
to fill the database like this:
This being said, below is my GET
method:
[Route("v1/recipe/image/{recipeId}")]
[HttpGet()]
public Image GetImage(int recipeId)
{
using (var con = _connFactory())
{
con.Open();
return con.Query<Image>("SELECT * FROM RecipeImage WHERE RecipeId = @recipeId", new { recipeId }).FirstOrDefault();
}
}
But I am getting the following error:
System.Data.DataException: 'Error parsing column 3 (Image=System.Byte[] - Object)'
InvalidCastException: Unable to cast object of type 'System.Byte[]' to type 'Microsoft.AspNetCore.Http.IFormFile'.
I understand what is going bad here, but I could not find any way around this. The only solution that I thought of was creating another model class for Image which instead of IFormFile
has byte[]
, but I was wondering if there's a better way of solving this problem.