public IActionResult DeleteImg(int id) { if (_uid == null) { return(RedirectToAction("Login", "Home")); } BlogImg img = dbContext.BlogImgs.FirstOrDefault(b => b.BlogImgId == id); // TODO remove images in batch using Checkboxes(?) // TODO prevent other edits from getting lost (i.e. title, content) dbContext.BlogImgs.Remove(img); dbContext.SaveChanges(); string fileLocation = Path.Combine(hostingEnvironment.WebRootPath, "img", img.ImgLoc); System.IO.File.Delete(fileLocation); return(RedirectToAction("EditForm", new { id = _blogId })); }
public void CreateBlogImgRows(int blogId, List <IFormFile> blogImgs) { if (blogImgs != null && blogImgs.Count > 0) { string uniqueFileName = null; string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "img"); foreach (IFormFile img in blogImgs) { uniqueFileName = Guid.NewGuid().ToString() + "_" + img.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); img.CopyTo(new FileStream(filePath, FileMode.Create)); BlogImg blogImg = new BlogImg(); blogImg.Add(blogId, uniqueFileName, img.FileName); dbContext.BlogImgs.Add(blogImg); dbContext.SaveChanges(); } } }