public ActionResult Details(IEnumerable <HttpPostedFileBase> files, int id) { //Ensure model state is valid if (ModelState.IsValid) { //iterating through multiple file collection int fileId = 0; foreach (HttpPostedFileBase file in files) { //Checking file is available to save. if (file != null) { var ext = Path.GetExtension(file.FileName); var InputFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + fileId + ext; var ServerSavePath = Path.Combine(Server.MapPath("~/Content/upload/") + InputFileName); //Save file to server folder file.SaveAs(ServerSavePath); Detail_Foto detail_Foto = new Detail_Foto(); detail_Foto.ID_Destinasi = id; detail_Foto.Foto = InputFileName; db.Detail_Foto.Add(detail_Foto); db.SaveChanges(); } fileId++; } } Destinasi destinasi = db.Destinasis.Find(id); //assigning file uploaded status to ViewBag for showing message to user. TempData["SuccessMessage"] = files.Count().ToString() + " foto berhasil diunggah."; return(View(destinasi)); }
public ActionResult HapusFoto(int detailId, int ID_Destinasi) { Detail_Foto detail = db.Detail_Foto.Find(detailId); string fullPath = Request.MapPath("~/Content/upload/" + detail.Foto); if (System.IO.File.Exists(fullPath)) { System.IO.File.Delete(fullPath); } db.Detail_Foto.Remove(detail); db.SaveChanges(); TempData["SuccessMessage"] = "Foto berhasil dihapus"; return(RedirectToAction("Details", "Destinasi", new { @id = ID_Destinasi })); }