using System; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Linq; using System.Text; namespace BLL { /// /// 上传附件相关 /// public class UploadFileService { #region 附件上传 /// /// 附件上传 /// /// 上传控件 /// 上传路径 /// 定义路径 /// public static string UploadAttachment(string rootPath, FineUIPro.FileUpload fileUpload, string fileUrl, string constUrl) { if (!string.IsNullOrEmpty(fileUrl)) ////是否存在附件 存在则删除 { string urlFullPath = rootPath + fileUrl; if (File.Exists(urlFullPath)) { File.Delete(urlFullPath); } } string initFullPath = rootPath + constUrl; if (!Directory.Exists(initFullPath)) { Directory.CreateDirectory(initFullPath); } string filePath = fileUpload.PostedFile.FileName; string fileName = Funs.GetNewFileName() + "~" + Path.GetFileName(filePath); int count = fileUpload.PostedFile.ContentLength; string savePath = constUrl + fileName; string fullPath = initFullPath + fileName; if (!File.Exists(fullPath)) { byte[] buffer = new byte[count]; Stream stream = fileUpload.PostedFile.InputStream; stream.Read(buffer, 0, count); MemoryStream memoryStream = new MemoryStream(buffer); FileStream fs = new FileStream(fullPath, FileMode.Create, FileAccess.Write); memoryStream.WriteTo(fs); memoryStream.Flush(); memoryStream.Close(); fs.Flush(); fs.Close(); memoryStream = null; fs = null; //if (!string.IsNullOrEmpty(fileUrl)) //{ // fileUrl += "," + savePath; //} //else //{ // fileUrl += savePath; //} fileUrl = savePath; } else { fileUrl = string.Empty; } return fileUrl; } #endregion #region 附件资源删除 /// /// 附件资源删除 /// /// /// public static void DeleteFile(string rootPath, string fileUrl) { if (!string.IsNullOrEmpty(fileUrl)) { string[] strs = fileUrl.Trim().Split(','); foreach (var item in strs) { string urlFullPath = rootPath + item; if (File.Exists(urlFullPath)) { File.Delete(urlFullPath); } } } } #endregion #region 上传文件路径 /// /// 用户-附件路径 /// public static string UserFilePath = "FileUpload\\User\\"; /// /// 安全组织体系 /// public const string HSSEOrganizeFilePath = "FileUpload\\HSSEOrganize\\"; /// /// 安全管理机构-附件路径 /// public const string HSSEManageFilePath = "FileUpload\\HSSEManage\\"; /// /// 标准规范-附件路径 /// public static string HSSEStandardsListFilePath = "FileUpload\\HSSEStandardsList\\"; /// ///法律法规-附件路径 /// public static string LawRegulationFilePath = "FileUpload\\LawRegulation\\"; /// /// 管理规定-附件路径 /// public static string ManageRuleFilePath = "FileUpload\\ManageRule\\"; /// /// 生产管理规章制度-附件路径 /// public static string RulesRegulationsFilePath = "FileUpload\\RulesRegulations\\"; /// /// 二维码上传路径 /// public const string QRCodeImageFilePath = "FileUpload\\QRCodeFile\\"; /// /// 培训教材库-附件路径 /// public static string TrainingFilePath = "FileUpload\\Training\\"; /// /// 事故案例库-附件路径 /// public static string AccidentCaseFilePath = "FileUpload\\AccidentCase\\"; /// /// HAZOP-附件路径 /// public static string HAZOPFilePath = "FileUpload\\HAZOP\\"; /// /// 安全试题库-附件路径 /// public static string TrainTestDBFilePath = "FileUpload\\TrainTestDB\\"; /// /// 安全专家-附件路径 /// public static string ExpertFilePath = "FileUpload\\Expert\\"; /// /// 应急预案-附件路径 /// public const string EmergencyFilePath = "FileUpload\\Emergency\\"; /// /// 安全评价 /// public const string AppraiseFilePath = "FileUpload\\Appraise\\"; /// /// 专项方案-附件路径 /// public const string SpecialSchemeFilePath = "FileUpload\\SpecialScheme\\"; /// /// 安全监督检查报告-附件路径 /// public static string SuperviseCheckReportFilePath = "FileUpload\\SuperviseCheckReport\\"; /// /// 安全生产快报-附件路径 /// public static string SafeProductionExpressFilePath = "FileUpload\\SafeProductionExpress\\"; /// /// 安全交流-附件路径 /// public static string ExchangeFilePath = "FileUpload\\Exchange\\"; /// /// 安全生产数据季报-附件路径 /// public static string SafetyQuarterlyReportFilePath = "FileUpload\\SafetyQuarterlyReport\\"; /// /// 在线督查 /// public const string OnLineSupervisionFilePath = "FileUpload\\OnLineSupervision\\"; /// /// 项目评价 /// public const string ProjectEvaluationFilePath = "FileUpload\\ProjectEvaluation\\"; /// /// 事故快报 /// public const string ProjectAccidentFilePath = "FileUpload\\ProjectAccident\\"; /// /// 评价报告 /// public const string SubUnitCheckRectifyFilePath = "FileUpload\\SubUnitCheckRectify\\"; /// /// 企业安全文件上报 /// public const string SubUnitReportFilePath = "FileUpload\\SubUnitReport\\"; /// /// 隐患整改通知单附件路径 /// public const string RectifyNoticeFilePath = "FileUpload\\RectifyNotice\\"; /// /// 工程暂停令附件路径 /// public const string PauseNoticeFilePath = "FileUpload\\PauseNotice\\"; /// /// 危险观察登记-附件路径 /// public static string RegistrationFilePath = "FileUpload\\Registration\\"; /// /// 分包方绩效评价-附件路径 /// public static string PerfomanceRecordFilePath = "FileUpload\\PerfomanceRecord\\"; /// /// 个人绩效评价-附件路径 /// public static string PersonPerfomanceFilePath = "FileUpload\\PersonPerfomance\\"; /// /// 奖励通知单-附件路径 /// public const string IncentiveNoticeFilePath = "FileUpload\\IncentiveNotice\\"; /// /// 处罚通知单-附件路径 /// public const string PunishNoticeFilePath = "FileUpload\\PunishNotice\\"; /// /// 获奖证书或奖杯--附件路径 /// public const string HSECertificateFilePath = "FileUpload\\HSECertificate\\"; /// /// 安全专职人员名单附件 /// public const string FullTimeManFilePath = "FileUpload\\FullTimeMan\\"; /// /// 项目经理人员名单附件 /// public const string PMManFilePath = "FileUpload\\PMMan\\"; #region 分包商资质 /// /// 营业执照扫描件-附件路径 /// public const string BL_ScanUrlFilePath = "FileUpload\\BL_ScanUrl\\"; /// /// 机构代码扫描件-附件路径 /// public const string O_ScanUrlFilePath = "FileUpload\\O_ScanUrl\\"; /// /// 资质证书扫描件-附件路径 /// public const string C_ScanUrlFilePath = "FileUpload\\C_ScanUrl\\"; /// /// 质量体系认证证书扫描件-附件路径 /// public const string QL_ScanUrlFilePath = "FileUpload\\QL_ScanUrl\\"; /// /// HSE体系认证证书扫描件-附件路径 /// public const string H_ScanUrlFilePath = "FileUpload\\H_ScanUrl\\"; /// /// HSE体系认证证书扫描件-附件路径 /// public const string H_ScanUrl2FilePath = "FileUpload\\H_ScanUrl2\\"; /// /// 安全生产许可证扫描件-附件路径 /// public const string SL_ScanUrlFilePath = "FileUpload\\SL_ScanUrl\\"; #endregion #region 采购供货厂家管理 /// // 培训记录-附件路径 /// public const string TrainRecordsUrlFilePath = "FileUpload\\TrainRecordsUrl\\"; /// /// 方案及资质审查-附件路径 /// public const string PlanUrlFilePath = "FileUpload\\PlanUrl\\"; /// /// 临时到场人员培训-附件路径 /// public const string TemporaryPersonUrlFilePath = "FileUpload\\TemporaryPersonUrl\\"; /// /// 厂家入场安全人员培训-附件路径 /// public const string InPersonTrainUrlFilePath = "FileUpload\\InPersonTrainUrl\\"; /// /// HSE协议-附件路径 /// public const string HSEAgreementUrlFilePath = "FileUpload\\HSEAgreementUrl\\"; #endregion #endregion /// /// 保存附件 /// /// /// /// /// public static void SaveAttachUrl(string source, string attachUrl,string menuId,string toKeyId) { string rootUrl = ConfigurationManager.AppSettings["localRoot"]; if (string.IsNullOrEmpty(rootUrl)) { rootUrl = Funs.RootPath; } List sour = (from x in Funs.DB.AttachFile where x.MenuId == menuId && x.ToKeyId == toKeyId select x).ToList(); if (sour.Count() == 0) { Model.AttachFile att = new Model.AttachFile { AttachFileId = SQLHelper.GetNewID(), ToKeyId = toKeyId, AttachSource = source.ToString(), AttachUrl = attachUrl, MenuId = menuId, // ImageByte= imageByte, //AttachPath= attachPath, }; if (menuId == BLL.Const.PersonListMenuId) { att.ImageByte = AttachFileService.SetImageToByteArray(rootUrl + attachUrl.Split(',')[0]); } Funs.DB.AttachFile.InsertOnSubmit(att); Funs.DB.SubmitChanges(); } else { Model.AttachFile att = Funs.DB.AttachFile.FirstOrDefault(x => x.MenuId == menuId && x.AttachFileId == sour.First().AttachFileId); if (att != null) { att.ToKeyId = toKeyId; att.AttachSource = source.ToString(); att.AttachUrl = attachUrl; att.MenuId = menuId; if (menuId == BLL.Const.PersonListMenuId) { att.ImageByte = AttachFileService.SetImageToByteArray(rootUrl + attachUrl.Split(',')[0]); } Funs.DB.SubmitChanges(); } } } /// /// 通过附件路径得到Source /// /// /// public static string GetSourceByAttachUrl(string attachUrl, int size, string oldSrouce) { string attachSource = string.Empty; if (!string.IsNullOrEmpty(attachUrl)) { attachUrl= attachUrl.Replace('/', '\\'); var allUrl = Funs.GetStrListByStr(attachUrl, ','); foreach (var item in allUrl) { int strInt = item.LastIndexOf("~"); if (strInt < 0) { strInt = item.LastIndexOf("\\"); } string folder = item.Substring(0,strInt + 1).Replace('\\', '/'); string name = item.Substring(strInt + 1); string type = item.Substring(item.LastIndexOf(".") + 1); string savedName = item.Substring(item.LastIndexOf("\\") + 1); string id = SQLHelper.GetNewID(typeof(Model.AttachFile)); attachSource += "{ \"name\": \"" + name + "\", \"folder\": \"" + folder + "\", \"type\": \"" + type + "\", \"savedName\": \"" + savedName + "\", \"size\": " + size + ", \"id\": \"" + SQLHelper.GetNewID(typeof(Model.AttachFile)) + "\" }@"; } attachSource = attachSource.Substring(0, attachSource.LastIndexOf("@")).Replace("@", ","); if (!string.IsNullOrEmpty(oldSrouce)) { attachSource = oldSrouce.Replace("]", ",") + attachSource + "]"; } else { attachSource = "[" + attachSource + "]"; } } return attachSource; } ////将虚拟路径转化为文件的路径然后最后转化为文件流 //public static ActionResult SaveImage(string path) //{ // var url = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + path; // FileStream fs = new FileStream(url, FileMode.Open, FileAccess.Read); //将图片以文件流的形式进行保存 // BinaryReader br = new BinaryReader(fs); // byte[] imgBytesIn = br.ReadBytes((int)fs.Length); //将流读入到字节数组中 // Encoding myEncoding = Encoding.GetEncoding("utf-8"); // string stImageByte = Convert.ToBase64String(imgBytesIn); // return Json(stImageByte); //} } }