Exemplo n.º 1
0
        //-------------------------------------------------------------------------------------------------
        //This return Success whether file is null or it's in a valid format
        //-------------------------------------------------------------------------------------------------
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var file = (IFormFile)value;


            if (file == null || file.Length == 0)
            {
                return(ValidationResult.Success);
            }


            // Get Default Constraints For Uploaded file to Validate it like {Allowed mimeTypes,MaxSize,Allowed Extentions}
            var Constraints = FileSettings.fileValidationConstraints(fileFormat);

            bool valid = ValidateFiles.IsValidFile(file, Constraints);

            if (valid)
            {
                return(ValidationResult.Success);
            }

            // UnValid Message
            string message = String.Format(ErrorMessage, String.Join('/', Constraints.AllowedExtension), (Constraints.FileMaximumBytes / 1024));

            return(new ValidationResult(message));
        }
Exemplo n.º 2
0
        public ActionResult Validate(ValidateFiles formData)
        {
            try
            {
                var parameterInstance = ParameterSingleton.Instance;

                var dataFiles   = Request.Files;
                var nameVersion =
                    $"{parameterInstance.ParameterList.FirstOrDefault(x => x.ParameterInternalIdentificator == ParameterInternalIdentificator.VersionValue).Value}.{DateTime.Now.Year.ToString().Substring(2)}.{DateTime.Now.Month}.{DateTime.Now.DayOfYear}.{DateTime.Now.Hour}";

                var path = $"{_directoryPath}/{nameVersion}";
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                for (int i = 0; i <= dataFiles.Count - 1; i++)
                {
                    var myfile = dataFiles[i];
                    if (myfile != null)
                    {
                        if (!_validateService.Validate(formData.FileTypeValidate, myfile.FileName, formData.RequiredBackup))
                        {
                            return(Json(new { Success = false, Message = $"El archivo {myfile.FileName} no cumple con las especificaciones de nombre o extensión" }, JsonRequestBehavior.DenyGet));
                        }

                        myfile.SaveAs($"{path}/{myfile.FileName}");
                    }
                }
                return(Json(new { Success = true, Message = "Archivo subido exitosamente" }, JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                Log.Instance.Error(ex);
                var result = new { Success = false, ex.Message };
                return(Json(result, JsonRequestBehavior.DenyGet));
            }
        }
Exemplo n.º 3
0
        public JsonResult Upload(ValidateFiles FileModel)
        {
            MSGReturnModel result = new MSGReturnModel();

            try
            {
                #region 前端無傳送檔案進來

                if (FileModel.File == null)
                {
                    result.RETURN_FLAG = false;
                    result.DESCRIPTION = Message_Type.upload_Not_Find.GetDescription();
                    return(Json(result));
                }

                #endregion 前端無傳送檔案進來

                #region 前端檔案大小不服或不為Excel檔案(驗證)

                if (!ModelState.IsValid)
                {
                    result.RETURN_FLAG = false;
                    result.DESCRIPTION = Message_Type.excel_Validate.GetDescription();
                    return(Json(result));
                }

                #endregion 前端檔案大小不服或不為Excel檔案(驗證)

                #region  傳檔案

                string pathType = Path.GetExtension(FileModel.File.FileName)
                                  .Substring(1);     //上傳的檔案類型

                var fileName = string.Format("{0}.{1}",
                                             Excel_UploadName.A71.GetDescription(),
                                             pathType);          //固定轉成此名稱

                Cache.Invalidate(CacheList.A71ExcelName);        //清除 Cache
                Cache.Set(CacheList.A71ExcelName, fileName, 15); //把資料存到 Cache

                #region 檢查是否有FileUploads資料夾,如果沒有就新增 並加入 excel 檔案

                string projectFile = Server.MapPath("~/" + SetFile.FileUploads); //專案資料夾
                string path        = Path.Combine(projectFile, fileName);
                FileRelated.createFile(projectFile);                             //檢查是否有FileUploads資料夾,如果沒有就新增

                //呼叫上傳檔案 function
                result = FileRelated.FileUpLoadinPath(path, FileModel.File);
                if (!result.RETURN_FLAG)
                {
                    return(Json(result));
                }

                #endregion 檢查是否有FileUploads資料夾,如果沒有就新增 並加入 excel 檔案

                #region 讀取Excel資料 使用ExcelDataReader 並且組成 json

                var stream = FileModel.File.InputStream;
                List <Exhibit29Model> dataModel = A7Repository.getExcel(pathType, stream);
                if (dataModel.Count > 0)
                {
                    result.RETURN_FLAG = true;
                    result.Datas       = Json(dataModel); //給JqGrid 顯示
                }
                else
                {
                    result.RETURN_FLAG = false;
                    result.DESCRIPTION = Message_Type.data_Not_Compare.GetDescription();
                }

                #endregion 讀取Excel資料 使用ExcelDataReader 並且組成 json

                #endregion  傳檔案
            }
            catch (Exception ex)
            {
                result.RETURN_FLAG = false;
                result.DESCRIPTION = Message_Type.upload_Fail
                                     .GetDescription(FileModel.File.FileName, ex.Message);
            }
            return(Json(result));
        }
Exemplo n.º 4
0
        public JsonResult UploadA96(ValidateFiles FileModel)
        {
            MSGReturnModel result = new MSGReturnModel();

            try
            {
                #region 前端無傳送檔案進來

                if (FileModel.File == null)
                {
                    result.RETURN_FLAG = false;
                    result.DESCRIPTION = Message_Type.upload_Not_Find.GetDescription();
                    return(Json(result));
                }

                #endregion 前端無傳送檔案進來

                #region 前端檔案大小不符或不為Excel檔案(驗證)

                //ModelState
                if (!ModelState.IsValid)
                {
                    result.RETURN_FLAG = false;
                    result.DESCRIPTION = Message_Type.excel_Validate.GetDescription();
                    return(Json(result));
                }

                #endregion 前端檔案大小不符或不為Excel檔案(驗證)

                #region  傳檔案

                string pathType = Path.GetExtension(FileModel.File.FileName)
                                  .Substring(1);      //上傳的檔案類型

                var fileName = string.Format("{0}.{1}",
                                             Excel_UploadName.A96.GetDescription(),
                                             pathType);      //固定轉成此名稱

                Cache.Invalidate(CacheList.A96ExcelName);    //清除 Cache
                Cache.Set(CacheList.A96ExcelName, fileName); //把資料存到 Cache

                #region 檢查是否有FileUploads資料夾,如果沒有就新增 並加入 excel 檔案

                string projectFile = Server.MapPath("~/" + SetFile.FileUploads); //專案資料夾
                string path        = Path.Combine(projectFile, fileName);

                FileRelated.createFile(projectFile); //檢查是否有FileUploads資料夾,如果沒有就新增

                //呼叫上傳檔案 function
                result = FileRelated.FileUpLoadinPath(path, FileModel.File);
                if (!result.RETURN_FLAG)
                {
                    return(Json(result));
                }

                #endregion 檢查是否有FileUploads資料夾,如果沒有就新增 並加入 excel 檔案

                #region 讀取Excel資料
                var data = A9Repository.getA96Excel(pathType, FileModel.File.InputStream);
                if (data.Item1.IsNullOrWhiteSpace())
                {
                    result.RETURN_FLAG = true;
                    Cache.Invalidate(CacheList.A96ExcelfileData);      //清除 Cache
                    Cache.Set(CacheList.A96ExcelfileData, data.Item2); //把資料存到 Cache
                }
                else
                {
                    result.RETURN_FLAG = false;
                    result.DESCRIPTION = data.Item1;
                }

                #endregion 讀取Excel資料

                #endregion  傳檔案
            }
            catch (Exception ex)
            {
                result.RETURN_FLAG = false;
                result.DESCRIPTION = ex.Message;
            }
            return(Json(result));
        }
Exemplo n.º 5
0
        public JsonResult UploadA59(ValidateFiles FileModel)
        {
            MSGReturnModel result = new MSGReturnModel();

            try
            {
                #region 前端無傳送檔案進來

                if (FileModel.File == null)
                {
                    result.RETURN_FLAG = false;
                    result.DESCRIPTION = Message_Type.upload_Not_Find.GetDescription();
                    return(Json(result));
                }

                #endregion 前端無傳送檔案進來

                #region 前端檔案大小不符或不為Excel檔案(驗證)

                //ModelState
                if (!ModelState.IsValid)
                {
                    result.RETURN_FLAG = false;
                    result.DESCRIPTION = Message_Type.excel_Validate.GetDescription();
                    return(Json(result));
                }

                #endregion 前端檔案大小不符或不為Excel檔案(驗證)

                #region  傳檔案

                string pathType = Path.GetExtension(FileModel.File.FileName)
                                  .Substring(1);      //上傳的檔案類型

                var fileName = string.Format("{0}.{1}",
                                             Excel_UploadName.A59.GetDescription(),
                                             pathType);      //固定轉成此名稱

                Cache.Invalidate(CacheList.A59ExcelName);    //清除 Cache
                Cache.Set(CacheList.A59ExcelName, fileName); //把資料存到 Cache

                #region 檢查是否有FileUploads資料夾,如果沒有就新增 並加入 excel 檔案

                string projectFile = Server.MapPath("~/" + SetFile.FileUploads); //專案資料夾
                string path        = Path.Combine(projectFile, fileName);

                FileRelated.createFile(projectFile); //檢查是否有FileUploads資料夾,如果沒有就新增

                //呼叫上傳檔案 function
                result = FileRelated.FileUpLoadinPath(path, FileModel.File);
                if (!result.RETURN_FLAG)
                {
                    return(Json(result));
                }

                #endregion 檢查是否有FileUploads資料夾,如果沒有就新增 並加入 excel 檔案

                #region 讀取Excel資料 使用ExcelDataReader 並且組成 json

                var stream = FileModel.File.InputStream;
                List <A59ViewModel> dataModel = new List <A59ViewModel>();
                var A59result = A5Repository.getA59Excel(pathType, path, "upload");
                if (A59result.Item1.IsNullOrWhiteSpace())
                {
                    dataModel = A59result.Item2;
                }
                else
                {
                    result.RETURN_FLAG = false;
                    result.DESCRIPTION = A59result.Item1;
                    return(Json(result));
                }
                if (dataModel.Count > 0)
                {
                    result.RETURN_FLAG = true;
                    var A59          = new A59ViewModel();
                    var jqgridParams = A59.TojqGridData();
                    result.Datas = Json(jqgridParams);
                    Cache.Invalidate(CacheList.A59ExcelfileData);     //清除 Cache
                    Cache.Set(CacheList.A59ExcelfileData, dataModel); //把資料存到 Cache
                }
                else
                {
                    result.RETURN_FLAG = false;
                    result.DESCRIPTION = Message_Type.data_Not_Compare.GetDescription();
                }

                #endregion 讀取Excel資料 使用ExcelDataReader 並且組成 json

                #endregion  傳檔案
            }
            catch (Exception ex)
            {
                result.RETURN_FLAG = false;
                result.DESCRIPTION = ex.Message;
            }
            return(Json(result));
        }