public ActionResult AddProductImage(int productId, HttpPostedFileBase image) { if (image == null) { return(Json(new { success = false, message = "Image not found, Upload Failed" })); } var product = _productService.GetProductById(productId); if (product == null) { return(Json(new { success = false, message = "Product not found, Upload failed" })); } _productImageService.AddProductImage(product, image); return(Json(new { success = true, message = "Image Uploaded Successfully" })); }
public bool UploadImages(List <IFormFile> files, Product newProduct, string directoryName) { try { string path = ""; if (files == null || files.Count == 0) { return(false); } else { foreach (var file in files) { string fileName = Path.GetFileNameWithoutExtension(file.FileName); string extension = Path.GetExtension(file.FileName); fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension; string pathToReturn = "~/Content/" + directoryName + "/" + fileName; path = Path.Combine( Directory.GetCurrentDirectory(), "wwwroot\\Content\\" + directoryName, fileName); using (var stream = new FileStream(path, FileMode.Create)) { file.CopyTo(stream); } //Insert the base64 data byte[] base64Bytes = FileToBase64(pathToReturn); //Add the image in the table productImage productImageService.AddProductImage(new ProductImage { ImagePath = pathToReturn, Product = newProduct, ImageBase64 = base64Bytes }); } return(true); } } catch (Exception ex) { throw ex; } }