public async Task <IActionResult> AddVideo(int id, string videoLink) { try { var content = new ProductTechnicalContent(); content.Content = videoLink; content.ContentType = ContentType.Video; content.ProductId = id; content.ContentOrder = await _db.ProductTechnicalContents .AsNoTracking() .Where(c => c.ProductId.Equals(id)).CountAsync(); await _db.ProductTechnicalContents.AddAsync(content); await _db.SaveChangesAsync(); HttpContext.Session.SetInt32("Message", (int)Messages.AddedVideoSuccessfully); return(RedirectToAction(actionName: "EditProduct", controllerName: "Products", routeValues: new { id = id, jumpTarget = "#product-description-content-list", area = "Admin" })); } catch (Exception) { throw; } }
public async Task <IActionResult> AddImage(int id, IFormFile image) { //Create images/product_content/large/product_id/ Folder string largePath = System.IO.Path.Combine(_host.WebRootPath, "images/product_content", "large", "product_" + id); if (!Directory.Exists(largePath)) { System.GC.Collect(); System.GC.WaitForPendingFinalizers(); Directory.CreateDirectory(largePath); } //Create images/product_content/medium/product_id/ Folder string mediumPath = System.IO.Path.Combine(_host.WebRootPath, "images/product_content", "medium", "product_" + id); if (!Directory.Exists(mediumPath)) { System.GC.Collect(); System.GC.WaitForPendingFinalizers(); Directory.CreateDirectory(mediumPath); } //Create images/product_content/small/product_id/ Folder string smallPath = System.IO.Path.Combine(_host.WebRootPath, "images/product_content", "small", "product_" + id); if (!Directory.Exists(smallPath)) { System.GC.Collect(); System.GC.WaitForPendingFinalizers(); Directory.CreateDirectory(smallPath); } try { //Upload new images: var imageName = "_" + System.DateTime.Now.ToString("YYYYMMDDhhmmssms") + Path.GetFileName(image.FileName); //Save new large image: var newLargePath = System.IO.Path.Combine(_host.WebRootPath, "images/product_content/large/product_" + id); var finalNewLargePath = Path.Combine(newLargePath, imageName); //path of medium image: var newMediumPath = System.IO.Path.Combine(_host.WebRootPath, "images/product_content/medium/product_" + id); var finalNewMediumPath = System.IO.Path.Combine(newMediumPath, imageName); //path of small image: var newSmallPath = System.IO.Path.Combine(_host.WebRootPath, "images/product_content/small/product_" + id); var finalNewSmallPath = System.IO.Path.Combine(newSmallPath, imageName); System.GC.Collect(); System.GC.WaitForPendingFinalizers(); using (var webPFileStream = new FileStream(finalNewLargePath, FileMode.Create)) { using (ImageFactory imageFactory = new ImageFactory(preserveExifData: false)) { imageFactory.Load(image.OpenReadStream()) .Resize(new Size(900, 0)) .Save(webPFileStream); } } using (var webPFileStream = new FileStream(finalNewMediumPath, FileMode.Create)) { using (ImageFactory imageFactory = new ImageFactory(preserveExifData: false)) { imageFactory.Load(image.OpenReadStream()) .Resize(new Size(600, 0)) .Save(webPFileStream); } } using (var webPFileStream = new FileStream(finalNewSmallPath, FileMode.Create)) { using (ImageFactory imageFactory = new ImageFactory(preserveExifData: false)) { imageFactory.Load(image.OpenReadStream()) .Resize(new Size(400, 0)) .Save(webPFileStream); } } var productContent = new ProductTechnicalContent(); productContent.ProductId = id; productContent.ContentType = ContentType.Image; productContent.Content = imageName; productContent.ContentOrder = await _db.ProductTechnicalContents.AsNoTracking() .Where(c => c.ProductId.Equals(id)).CountAsync(); await _db.ProductTechnicalContents.AddAsync(productContent); await _db.SaveChangesAsync(); HttpContext.Session.SetInt32("Message", (int)Messages.AddedImageSuccessfully); return(RedirectToAction(actionName: "EditProduct", controllerName: "Products", routeValues: new { id = id, jumpTarget = "#product-description-content-list", area = "Admin" })); } catch (Exception e) { string m = e.Message; return(Content(m)); } }