My code only saves first input image. I found this link to see how I can handle with multiple images. If I change HttpPostedFileBase to HttpPostedFileBase[], I cannot use InputStream and ContentLength for HttpPostedFileBase image
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Name,Color,CategoryId,GenderId,Image")] Product product, HttpPostedFileBase image)
{
if (ModelState.IsValid)
{
byte[] imagee = null;
if (Request.Files.Count > 0)
{
image = Request.Files[0];
using (BinaryReader br = new BinaryReader(image.InputStream))
{
imagee = br.ReadBytes(image.ContentLength);
}
}
product.Image = imagee;
db.Products.Add(product);
db.SaveChanges();
return RedirectToAction("Index");
}
I show the only one image like this:
<img class="img-rounded img-responsive" src="data:image/jpg;base64,@System.Convert.ToBase64String(item.Image)" width="250" height="400" />
How can I store multiple images and also show them?
Request.Files[0]- your method already has a parameter for the files. If itsIEnumerable<HttpPostedFileBase> image, then you just loop through the files -foreach(HtttpPostedFileBase file in image) { ....definition for 'InputStream' and no extension method 'InputStream' accepting a first argument of type 'HttpPostedFileBase[]' could be foundand same error forContentLength. Our professor shared this code but I want to extend the code as storing multiple images. According to your comment, do I just useforeachto get every input?HttpPostedFileBase[]is a collection - you need to loop though the collection to get each file in it. And of course yourproduct.Imagewont work since that only holds one image. You need a second database table to store the images (with a FK to the Product ID)