Example #1
0
 public IActionResult addProduct([Bind("Title, Description, Price")] Product produit, List <IFormFile> photos, List <int> Categories)
 {
     foreach (IFormFile image in photos)
     {
         string imageFile = Path.Combine("wwwroot", "images", produit.Title + "-" + Path.GetFileName(image.FileName));
         string urlImage  = "http://" + Request.Host + "/images/" + produit.Title + "-" + Path.GetFileName(image.FileName);
         //var stream = System.IO.File.Create(imageFile);
         //image.CopyTo(stream);
         //stream.Close();
         BinaryReader reader = new BinaryReader(image.OpenReadStream());
         serviceImage.SaveImage(reader.ReadBytes((int)image.Length), imageFile, 250, 250);
         Image img = new Image {
             UrlImage = urlImage
         };
         data.Images.Add(img);
         data.SaveChanges();
         produit.Images.Add(new ImageProduit {
             Product = produit, Image = img
         });
     }
     foreach (int c in Categories)
     {
         produit.Categories.Add(new ProductCategorie {
             Categorie = data.Categories.Find(c), Product = produit
         });
     }
     data.Products.Add(produit);
     data.SaveChanges();
     return(RedirectToAction("Index"));
 }