コード例 #1
0
        public JsonResult LoadFileAttachment(int attachmentId)
        {
            Logger.Info(_logMsg.Clear().SetPrefixMsg("Load FileAttachment").ToInputLogString());

            try
            {
                _commPoolFacade = new CommPoolFacade();
                AttachmentEntity selectedAttach = _commPoolFacade.GetAttachmentsById(attachmentId);
                TempData["FILE_DOWNLOAD"] = selectedAttach;

                _commonFacade = new CommonFacade();
                string documentFolder = _commonFacade.GetJobDocumentFolder();
                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 FileAttachment").Add("Error Message", ex.Message).ToFailLogString());
                return(Json(new
                {
                    Valid = false,
                    Error = Resource.Error_System,
                    Errors = string.Empty
                }));
            }
        }
コード例 #2
0
        public ActionResult PreviewAttachment()
        {
            Logger.Info(_logMsg.Clear().SetPrefixMsg("Preview FileAttachment").ToInputLogString());

            try
            {
                AttachmentEntity selectedAttach = (AttachmentEntity)TempData["FILE_DOWNLOAD"];
                TempData["FILE_DOWNLOAD"] = selectedAttach; // keep object

                _commonFacade = new CommonFacade();
                string documentFolder = _commonFacade.GetJobDocumentFolder();
                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 FileAttachment").Add("Error Message", ex.Message).ToFailLogString());
                return(Error(new HandleErrorInfo(ex, this.ControllerContext.RouteData.Values["controller"].ToString(),
                                                 this.ControllerContext.RouteData.Values["action"].ToString())));
            }
        }