public ActionResult DeleteAttachStation(int sid) { TblKptStation st = db.TblKptStation.Find(sid); try { SysRole role = CurrentUser.Role(3).Role(); bool canDelete = role.SysRolePermission.Where(x => x.MenuId == 3220 && x.PermissionId == 3 && x.PermissionFlag).Count() > 0; if (!canDelete) { return(RedirectToAction("Station", "KPT", new { msg = "ไม่มีสิทธิ์ในการลบข้อมูล", msgType = AlertMsgType.Danger, id = st.StationId, kId = st.KptId })); } int id = Request.Form["Id"].ParseInt(); TblKptStationAttachment ob = db.TblKptStationAttachment.Find(id); if (ob == null) { return(RedirectToAction("Station", "KPT", new { msg = "ไม่พบข้อมูลที่ต้องการ", msgType = AlertMsgType.Warning, id = st.StationId, kId = st.KptId })); } string filePath = ob.AttachPath; db.TblKptStationAttachment.Remove(ob); db.SaveChanges(); if (FileIO.Exists(Server.MapPath("~/" + filePath))) { FileIO.Delete(Server.MapPath("~/" + filePath)); } return(RedirectToAction("Station", "KPT", new { msg = "ลบข้อมูลเรียบร้อยแล้ว", msgType = AlertMsgType.Success, id = st.StationId, kId = st.KptId })); } catch (Exception ex) { return(RedirectToAction("Station", "KPT", new { msg = ex.GetMessage(), msgType = AlertMsgType.Danger, id = st.StationId, kId = st.KptId })); } }
public ActionResult SetAttachStation() { int Id = Request.Form["StationId"].ParseInt(); //SysRole role = CurrentUser.Role(3).Role(); //ViewBag.canWrite = role.SysRolePermission.Where(x => x.MenuId == 3220 && x.PermissionId == 2 && x.PermissionFlag).Count() > 0; //ViewBag.canDelete = role.SysRolePermission.Where(x => x.MenuId == 3220 && x.PermissionId == 3 && x.PermissionFlag).Count() > 0; //ViewBag.canExport = role.SysRolePermission.Where(x => x.MenuId == 3220 && x.PermissionId == 4 && x.PermissionFlag).Count() > 0; TblKptStation ob = db.TblKptStation.Include(x => x.TblKpt).Where(x => x.StationId == Id).FirstOrDefault(); try { TblKptStationAttachment attach = new TblKptStationAttachment() { TblKptStation = ob, AttachName = Request.Form["AttachName"].Convert2String() }; if (Request.Files.Count > 0 && Request.Files["AttachFile"] != null && Request.Files["AttachFile"].ContentLength > 0) { HttpPostedFileBase uploadedFile = Request.Files["AttachFile"]; string FilePath = string.Format("files/kpt/{0}{1}", CurrentDate.ParseString(DateFormat._yyyyMMddHHmmssfff), Path.GetExtension(uploadedFile.FileName)); if (!Directory.Exists(Server.MapPath("~/files"))) { Directory.CreateDirectory(Server.MapPath("~/files")); } if (!Directory.Exists(Server.MapPath("~/files/kpt"))) { Directory.CreateDirectory(Server.MapPath("~/files/kpt")); } uploadedFile.SaveAs(Server.MapPath("~/" + FilePath)); attach.AttachPath = FilePath; if (string.IsNullOrWhiteSpace(attach.AttachName)) { attach.AttachName = Path.GetFileNameWithoutExtension(uploadedFile.FileName); } } else { throw new Exception("กรุณาอัพโหลดไฟล์แนบ"); } db.TblKptStationAttachment.Add(attach); db.SaveChanges(); return(RedirectToAction("Station", "KPT", new { kId = ob.TblKpt.KptId, id = ob.StationId, msg = "บันทึกข้อมูลเรียบร้อยแล้ว", msgType = AlertMsgType.Success })); //return ViewDetail(ob, "บันทึกข้อมูลเรียบร้อยแล้ว", AlertMsgType.Success); } catch (Exception ex) { string msg = ex.GetMessage(); return(RedirectToAction("Station", "KPT", new { kId = ob.TblKpt.KptId, id = ob.StationId, msg, msgType = AlertMsgType.Danger })); } }