I need to pass an image within a json response. Here's my controller:
public IHttpActionResult GetStudents()
{
var data = db.Students.ToList();
return Ok(data);
}
That code obviously returns all the data for each student and returns the student_image column as varbinary but I need it to be passed as an image.
I can return the image if it's the only thing to be returned, and here's the code:
public HttpResponseMessage GetStudentImages(string id)
{
var img = (from s in db.Students select new { s.student_image, s.student_id }).Where(a => a.student_id == id).FirstOrDefault();
var result = new HttpResponseMessage();
if (img == null)
{
result.StatusCode = HttpStatusCode.NotFound;
}
else
{
result.Content = new ByteArrayContent(img.student_image);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
}
return result;
}
How can I pass the image inside the response ? Thanks.