public JsonResult LoadFileNewsAttachment(int attachmentId) { Logger.Info(_logMsg.Clear().SetPrefixMsg("Load FileNewsAttachment").ToInputLogString()); try { if (TempData["NewsAttachmentList"] != null) { var lstNewsAttachment = (List <AttachmentEntity>)TempData["NewsAttachmentList"]; TempData["NewsAttachmentList"] = lstNewsAttachment; // keep for download if (lstNewsAttachment != null) { AttachmentEntity selectedAttach = lstNewsAttachment.FirstOrDefault(x => x.AttachmentId == attachmentId); TempData["FILE_DOWNLOAD"] = selectedAttach; _commonFacade = new CommonFacade(); string documentFolder = _commonFacade.GetNewsDocumentFolder(); string pathFile = string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", documentFolder, selectedAttach.Url); if (!System.IO.File.Exists(pathFile)) { return(Json(new { Valid = false, Error = "ไม่พบไฟล์ที่ต้องการ Download", Errors = string.Empty })); } } } return(Json(new { Valid = true })); } catch (Exception ex) { Logger.Error("Exception occur:\n", ex); Logger.Info(_logMsg.Clear().SetPrefixMsg("Load FileNewsAttachment").Add("Error Message", ex.Message).ToFailLogString()); return(Json(new { Valid = false, Error = Resource.Error_System, Errors = string.Empty })); } }
public ActionResult PreviewNewsAttachment() { Logger.Info(_logMsg.Clear().SetPrefixMsg("Preview NewsAttachment").ToInputLogString()); try { AttachmentEntity selectedAttach = (AttachmentEntity)TempData["FILE_DOWNLOAD"]; _commonFacade = new CommonFacade(); string documentFolder = _commonFacade.GetNewsDocumentFolder(); string pathFile = string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", documentFolder, selectedAttach.Url); byte[] byteArray = System.IO.File.ReadAllBytes(pathFile); return(File(byteArray, selectedAttach.ContentType, selectedAttach.Filename)); } catch (Exception ex) { Logger.Error("Exception occur:\n", ex); Logger.Info(_logMsg.Clear().SetPrefixMsg("Preview NewsAttachment").Add("Error Message", ex.Message).ToFailLogString()); return(Error(new HandleErrorInfo(ex, this.ControllerContext.RouteData.Values["controller"].ToString(), this.ControllerContext.RouteData.Values["action"].ToString()))); } }
public ActionResult Edit(NewsViewModel newsVM) { Logger.Info(_logMsg.Clear().SetPrefixMsg("Save News").Add("NewsId", newsVM.NewsId) .Add("Topic", newsVM.Topic).ToInputLogString()); try { #region "Validation" bool isValid = TryUpdateModel(newsVM); string content = ApplicationHelpers.StripHtmlTags(newsVM.Content); if (string.IsNullOrWhiteSpace(content)) { isValid = false; ModelState.AddModelError("Content", string.Format(CultureInfo.InvariantCulture, Resource.ValErr_RequiredField, Resource.Lbl_Content)); } if (!string.IsNullOrEmpty(newsVM.AnnounceDate) && !newsVM.AnnounceDateValue.HasValue) { isValid = false; ModelState.AddModelError("AnnounceDate", Resource.ValErr_InvalidDate); } else if (newsVM.AnnounceDateValue.HasValue) { // เช็คห้ามเลือกวันที่ย้อนหลังเฉพาะกรณี เพิ่มข้อมูลใหม่ if ((newsVM.NewsId.HasValue == false || newsVM.NewsId == 0) && newsVM.AnnounceDateValue.Value < DateTime.Now.Date) { isValid = false; ModelState.AddModelError("AnnounceDate", Resource.ValErr_InvalidDate_MustMoreThanToday); } } if (!string.IsNullOrEmpty(newsVM.ExpiryDate) && !newsVM.ExpiryDateValue.HasValue) { isValid = false; ModelState.AddModelError("ExpiryDate", Resource.ValErr_InvalidDate); } else if (newsVM.ExpiryDateValue.HasValue) { // เช็คห้ามเลือกวันที่ย้อนหลังเฉพาะกรณี เพิ่มข้อมูลใหม่ if ((newsVM.NewsId.HasValue == false || newsVM.NewsId == 0) && newsVM.ExpiryDateValue.Value < DateTime.Now.Date) { isValid = false; ModelState.AddModelError("ExpiryDate", Resource.ValErr_InvalidDate_MustMoreThanToday); } } if (newsVM.AnnounceDateValue.HasValue && newsVM.ExpiryDateValue.HasValue && newsVM.AnnounceDateValue.Value > newsVM.ExpiryDateValue.Value) { isValid = false; ModelState.AddModelError("AnnounceDate", Resource.ValErr_InvalidDateRange); ModelState.AddModelError("ExpiryDate", ""); } #endregion if (isValid) { List <NewsBranchEntity> selectedBranch = newsVM.SelectedBranch; // Validate select at least one branch if (!newsVM.SelectedBranch.Any(x => x.IsDelete == false)) { ViewBag.ErrorMessage = Resource.ValErr_AtLeastOneItem; goto Outer; } // Validate MaxLength if (newsVM.Content.Count() > Constants.MaxLength.NewsContent) { ModelState.AddModelError("Content", string.Format(CultureInfo.InvariantCulture, Resource.ValErr_StringLength, Resource.Lbl_Content, Constants.MaxLength.NewsContent)); goto Outer; } // Save News NewsEntity newsEntity = new NewsEntity { NewsId = newsVM.NewsId, Topic = newsVM.Topic, AnnounceDate = newsVM.AnnounceDate.ParseDateTime(Constants.DateTimeFormat.DefaultShortDate), ExpiryDate = newsVM.ExpiryDate.ParseDateTime(Constants.DateTimeFormat.DefaultShortDate), Content = newsVM.Content, Status = newsVM.Status, CreateUserId = this.UserInfo.UserId, UpdateUserId = this.UserInfo.UserId }; _commonFacade = new CommonFacade(); newsEntity.DocumentFolder = _commonFacade.GetNewsDocumentFolder(); _newsFacade = new NewsFacade(); bool success = _newsFacade.SaveNews(newsEntity, selectedBranch, newsVM.AttachmentList); if (success) { return(RedirectToAction("Search", "News")); } ViewBag.ErrorMessage = Resource.Error_SaveFailed; } Outer: TempData["NewsVM"] = newsVM; return(InitEdit()); } catch (Exception ex) { Logger.Error("Exception occur:\n", ex); Logger.Info(_logMsg.Clear().SetPrefixMsg("Save News").Add("Exception occur:\n", ex).ToFailLogString()); return(Error(new HandleErrorInfo(ex, this.ControllerContext.RouteData.Values["controller"].ToString(), this.ControllerContext.RouteData.Values["action"].ToString()))); } }