If I call the GetImage action method directly ie: localserver/admin/GetImage?ProductID=36 it prompts me to download the file. Upon download, even when the file has the correct extension it does not open, testing with regular *.jpeg images from my pictures folder.
Also how do I set the filename when saving the file to database, do I need to create additional varchar type etc. field in database just for the filename ?
Once assigned to productMedia.ImageData the data is also all zeroes, hence null. Unlike the “image”, which has varying digits in there. Maybe it should be image.InputStream.Write and not image.InputStream.Read ? Tried it but still no go.
View
@using (Html.BeginForm("SaveProduct", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))
Here’s the saving procedure:
if (image != null)
{
var ProductMedia = new ProductMedia();
ProductMedia.ImageMimeType = image.ContentType;
ProductMedia.ImageData = new byte[image.ContentLength];
image.InputStream.Read(ProductMedia.ImageData, 0, image.ContentLength);
ProductMedia.ProductID = product.ProductID;
context.ProductMedias.AddObject(ProductMedia);
context.SaveChanges();
}
and the GetImage method:
public FileContentResult GetImage(int productID)
{
var ProductImages = context.ProductMedias.FirstOrDefault(x => x.ProductID == productID);
if (ProductImages != null)
{
return File(ProductImages.ImageData, ProductImages.ImageMimeType);
}
else
{
return null;
}
}
Nothing wrong with your
GetImagemethod check your saving procedure for correct content type correct data. And in database check whether you have enough size for the image inImageDatacolumn.For the second question , Yes you need a separate column for file name.