public ActionResult DeletePorto(int Id_Portofolio) { var porto = db.Portofolios.Find(Id_Portofolio); string currentImg = Request.MapPath(porto.ImagePath); db.Entry(porto).State = EntityState.Deleted; if (db.SaveChanges() > 0) { if (System.IO.File.Exists(currentImg)) { System.IO.File.Delete(currentImg); } return(RedirectToAction("Index")); } return(View()); }
public ActionResult UpdatePicture(ProfileViewModel model) { var file = model.ImageUpload; byte[] imagebyte = null; if (file != null && model.Id_User > 0) { string filename = Path.GetFileNameWithoutExtension(file.FileName); string extension = Path.GetExtension(file.FileName); filename = filename + extension; BinaryReader reader = new BinaryReader(file.InputStream); imagebyte = reader.ReadBytes(file.ContentLength); User iuser = new User(); iuser = db.Users.Where(x => x.Id_User == model.Id_User).FirstOrDefault(); iuser.Image_Byte = imagebyte; iuser.Image_Path = "/Images/Profile/" + filename; iuser.Picture = filename; db.Entry(iuser).State = EntityState.Modified; string oldImgPath = Request.MapPath(Session["AdminImagePath"].ToString()); if (db.SaveChanges() > 0) { file.SaveAs(Server.MapPath("/Images/Profile/" + filename)); if (System.IO.File.Exists(oldImgPath)) { System.IO.File.Delete(oldImgPath); } } } return(RedirectToAction("Index")); }