Having an issue where my images appear as errors. Tried both methods of converting a byte array to an image. Any help is appreciated.
View:
if (product.ImageUploadBytes != null)
{
var base64 = Convert.ToBase64String(product.ImageUploadBytes);
var imgSrc = String.Format("data:image/gif;base64,{0}", base64);
<div class="panel-body"><img src=@imgSrc /></div>
<img src="@Url.Action("GetImage", "Products", new { id = product.ID })" />
}
Controller:
public ActionResult GetImage(int imageID)
{
Product p = new Product();
p = _context.Products.FirstOrDefault(x => x.ID == imageID);
if (p.ImageUploadBytes != null)
{
return File(p.ImageUploadBytes, "image/png");
}
else
{
return null;
}
}
Byte Array in DB: Image
Image Uploading to DB:
[HttpPost]
public ActionResult AddImage(Product model, HttpPostedFileBase image)
{
if (image != null)
{
model.ImageUploadBytes = new byte[image.ContentLength];
image.InputStream.Read(model.ImageUploadBytes, 0, image.ContentLength);
}
_context.Products.Add(model);
_context.SaveChanges();
return View(model);
}