When I try to display a byte[] array image inside a Details page template using:
public FileContentResult RenderPhoto(byte[] photo)
{
// var array = (byte[])Session["photo"];
// return File(array, "image/jpeg");
return File(photo, "image/jpeg");
}
<img src="@Url.Action("RenderPhoto", Model.Photo)"/>
photo is null.
If I store student.Photo in Session:
//
// GET: /Student/Details/5
public ViewResult Details(int id)
{
Student student = db.Students.Find(id);
Session["photo"] = student.Photo;
return View(student);
}
and try to display the image retrieving the value from Session (commented lines above) it works.
Why I'm getting a null value in the first case?
After passing student to the View in ViewResult Details(int id)
, Model.Photo
doesn't hold that value anymore?
You cannot pass a byte array to the server in an
<img>
tag. The<img>
tag simply sends a GET request to the designated resource. The correct way to do this is to use an id:and then: