public async Task <IActionResult> AddVideo(int id, string videoLink) { try { var content = new AboutUsContent(); content.Content = videoLink; content.ContentType = ContentType.Video; content.AboutId = id; content.ContentOrder = await _db.AboutUsContents .AsNoTracking() .Where(c => c.AboutId.Equals(id)).CountAsync(); await _db.AboutUsContents.AddAsync(content); await _db.SaveChangesAsync(); HttpContext.Session.SetInt32("Message", (int)Messages.AddedVideoSuccessfully); return(RedirectToAction(actionName: "Index", controllerName: "AboutUs", routeValues: new { jumpTarget = "#about-description-content-list", area = "Admin_EN" })); } catch (Exception) { throw; } }
public async Task <IActionResult> AddImage(int id, IFormFile image) { //Create images/about_content/large Folder string largePath = System.IO.Path.Combine(_host.WebRootPath, "images/about_content", "large"); if (!Directory.Exists(largePath)) { Directory.CreateDirectory(largePath); } //Create images/about_content/medium Folder string mediumPath = System.IO.Path.Combine(_host.WebRootPath, "images/about_content", "medium"); if (!Directory.Exists(mediumPath)) { Directory.CreateDirectory(mediumPath); } //Create images/about_content/small Folder string smallPath = System.IO.Path.Combine(_host.WebRootPath, "images/about_content", "small"); if (!Directory.Exists(smallPath)) { Directory.CreateDirectory(smallPath); } try { //Upload new images: var imageName = "_" + System.DateTime.Now.ToString("yyyyMMddhhmmss") + Path.GetFileName(image.FileName); //Save new large image: var newLargePath = System.IO.Path.Combine(_host.WebRootPath, "images/about_content/large"); var finalNewLargePath = Path.Combine(newLargePath, imageName); //path of medium image: var newMediumPath = System.IO.Path.Combine(_host.WebRootPath, "images/about_content/medium"); var finalNewMediumPath = System.IO.Path.Combine(newMediumPath, imageName); //path of small image: var newSmallPath = System.IO.Path.Combine(_host.WebRootPath, "images/about_content/small"); 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(1200, 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(800, 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 aboutContent = new AboutUsContent(); aboutContent.AboutId = id; aboutContent.ContentType = ContentType.Image; aboutContent.Content = imageName; aboutContent.ContentOrder = await _db.AboutUsContents.AsNoTracking() .Where(c => c.AboutId.Equals(id)).CountAsync(); await _db.AboutUsContents.AddAsync(aboutContent); await _db.SaveChangesAsync(); HttpContext.Session.SetInt32("Message", (int)Messages.AddedImageSuccessfully); return(RedirectToAction(actionName: "Index", controllerName: "AboutUs", routeValues: new { jumpTarget = "#about-description-content-list", area = "Admin" })); } catch (Exception e) { return(Json(false)); } }