Reputation: 308
My image storing format is File -> byte[]
-> string base64
-> varbinary(max)
.
My image retrieve format is varbinary(max)
-> byte[]
-> then image format.
But I can not display the image.
Please help retrieve the image format.
Upvotes: 1
Views: 7511
Reputation: 246
What mime type of your image?
Maybe you need a method Controller.File(fileContents: yourByteArray, contentType: "image/jpeg")
For example:
// Represent a image.
public class MyImage
{
public ID { get; set; }
public byte[] Data { get; set; }
public string MimeType { get; set; }
}
Upload image:
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
if (ModelState.IsValid)
{
if (file != null)
{
var image = new MyImage()
{
MimeType = file.ContentType,
Data = new byte[file.ContentLength]
}
file.InputStream.Read(image.Data, 0, file.ContentLength);
}
repository.SaveImage(image);
return RedirectToAction("Index");
}
else
{
return View(product);
}
}
Get image:
public FileContentResult GetImage(int imageID)
{
var image = repository.Images.FirstOrDefault(p => p.ID == imageID);
if (image != null)
{
return File(image.Data, image.MimeType);
}
else
return null;
}
View: show image
@model YourApp.Entities.MyImage
<div class="item">
<img src="@Url.Action("GetImage", "YourController",
new { Model.ID })" />
</div>
Upvotes: 3