public static string GetCS(string cs, string mode, Configs config) { if (string.IsNullOrEmpty(cs) || config.ConnectionStrings.Any(x => x.Key.ToLower() == cs.ToLower()) == false) { cs = "default"; } int index = cs.LastIndexOf("_"); if (index > 0) { cs = cs.Substring(0, index); } if (mode?.ToLower() == "read") { var reads = config.ConnectionStrings.Where(x => x.Key.StartsWith(cs + "_")).Select(x => x.Key).ToList(); if (reads.Count > 0) { Random r = new Random(); var v = r.Next(0, reads.Count); cs = reads[v]; } } return(cs); }
/// <summary> /// 上传文件并返回FileAttachmentVM(后台代码使用) /// </summary> /// <param name="vm"></param> /// <param name="FileData"></param> /// <param name="con"></param> /// <param name="FileName"></param> /// <param name="savePlace"></param> /// <param name="groupName"></param> /// <returns></returns> public static FileAttachmentVM GetFileByteForUpload(FileAttachmentVM vm, Stream FileData, Configs con, string FileName = null, SaveFileModeEnum?savePlace = null, string groupName = null) { savePlace = savePlace == null ? con.FileUploadOptions.SaveFileMode : savePlace; var ext = string.Empty; if (string.IsNullOrEmpty(FileName) == false) { var dotPos = FileName.LastIndexOf('.'); ext = FileName.Substring(dotPos + 1); } vm.Entity.FileName = FileName; vm.Entity.FileExt = ext; if (savePlace == SaveFileModeEnum.Database) { using (var dataStream = new MemoryStream()) { FileData.CopyTo(dataStream); vm.Entity.FileData = dataStream.ToArray(); } } else if (savePlace == SaveFileModeEnum.Local) { string pathHeader = con.FileUploadOptions.UploadDir; if (!Directory.Exists(pathHeader)) { Directory.CreateDirectory(pathHeader); } var fullPath = Path.Combine(con.FileUploadOptions.UploadDir, $"{Guid.NewGuid().ToNoSplitString()}.{vm.Entity.FileExt}"); using (var fileStream = File.Create(fullPath)) { FileData.CopyTo(fileStream); vm.Entity.Path = fullPath; vm.Entity.FileData = null; } } else if (savePlace == SaveFileModeEnum.DFS) { using (var dataStream = new MemoryStream()) { StorageNode node = null; FileData.CopyTo(dataStream); if (!string.IsNullOrEmpty(groupName)) { node = FDFSClient.GetStorageNode(groupName); } else { node = FDFSClient.GetStorageNode(); } if (node != null) { vm.Entity.Path = "/" + FDFSClient.UploadFile(node, dataStream.ToArray(), vm.Entity.FileExt); vm.Entity.GroupName = node.GroupName; } vm.Entity.FileData = null; } FileData.Dispose(); } return(vm); }
/// <summary> /// 通过FileId获取其文件流 /// </summary> /// <param name="fileid"></param> /// <param name="dc"></param> /// <param name="con"></param> /// <returns></returns> public static byte[] GetFileByteForDownLoadById(Guid fileid, IDataContext dc, Configs con) { byte[] data = null; FileAttachment fi = dc.Set <FileAttachment>().Where(x => x.ID == fileid).FirstOrDefault(); if (fi != null) { SaveFileModeEnum saveMode = fi.SaveFileMode == null ? con.FileUploadOptions.SaveFileMode : fi.SaveFileMode.Value; data = GetBytes(saveMode, fi, dc); } return(data); }
/// <summary> /// 根据定义的SaveFileModeEnum模式,获取File信息放入XSSFWorkbook中 /// </summary> /// <param name="xssfworkbook"></param> /// <param name="fa"></param> /// <param name="con"></param> /// <returns></returns> public static XSSFWorkbook GetXSSFWorkbook(XSSFWorkbook xssfworkbook, FileAttachment fa, Configs con) { var saveMode = fa.SaveFileMode == null ? con.FileUploadOptions.SaveFileMode : fa.SaveFileMode; if (saveMode == SaveFileModeEnum.Database) { using (MemoryStream ms = new MemoryStream(fa.FileData)) { xssfworkbook = new XSSFWorkbook(ms); } } if (saveMode == SaveFileModeEnum.Local) { using (FileStream file = new FileStream(fa.Path, FileMode.Open, FileAccess.Read)) { xssfworkbook = new XSSFWorkbook(file); } } if (saveMode == SaveFileModeEnum.DFS) { using (MemoryStream ms = new MemoryStream(FDFSClient.DownloadFile(fa.GroupName, fa.Path.TrimStart('/')))) { xssfworkbook = new XSSFWorkbook(ms); } } return(xssfworkbook); }
/// <summary> /// 上传文件并返回FileAttachmentVM(后台代码使用) /// </summary> /// <param name="vm"></param> /// <param name="FileData"></param> /// <param name="con"></param> /// <param name="FileName"></param> /// <param name="savePlace"></param> /// <returns></returns> public static FileAttachmentVM GetFileByteForUpload(FileAttachmentVM vm, Stream FileData, Configs con, string FileName = null, SaveFileModeEnum?savePlace = null) { savePlace = savePlace == null ? con.SaveFileMode : savePlace; var ext = string.Empty; if (string.IsNullOrEmpty(FileName) == false) { var dotPos = FileName.LastIndexOf('.'); ext = FileName.Substring(dotPos + 1); } vm.Entity.FileName = FileName; vm.Entity.FileExt = ext; if (savePlace == SaveFileModeEnum.Database) { using (var dataStream = new System.IO.MemoryStream()) { FileData.CopyTo(dataStream); vm.Entity.FileData = new FileAttachmentData(); vm.Entity.FileData.FileData = dataStream.ToArray(); } } else if (savePlace == SaveFileModeEnum.Local) { string pathHeader = con.UploadDir; if (!Directory.Exists(pathHeader)) { Directory.CreateDirectory(pathHeader); } var fullPath = $"{pathHeader}/{Guid.NewGuid().ToNoSplitString()}.{vm.Entity.FileExt}"; using (var fileStream = File.Create(fullPath)) { FileData.CopyTo(fileStream); vm.Entity.Path = fullPath; vm.Entity.FileData = new FileAttachmentData(); } } FileData.Dispose(); return(vm); }
/// <summary> /// 下载文件 HSSFWorkbook /// </summary> /// <param name="hssfworkbook"></param> /// <param name="fa"></param> /// <param name="con"></param> /// <returns></returns> public static HSSFWorkbook GetHSSWorkbook(HSSFWorkbook hssfworkbook, FileAttachment fa, Configs con) { var saveMode = fa.SaveFileMode == null ? con.SaveFileMode : fa.SaveFileMode; if (saveMode == SaveFileModeEnum.Database) { using (MemoryStream ms = new MemoryStream(fa.FileData.FileData)) { hssfworkbook = new HSSFWorkbook(ms); } } if (saveMode == SaveFileModeEnum.Local) { using (FileStream file = new FileStream(fa.Path, FileMode.Open, FileAccess.Read)) { hssfworkbook = new HSSFWorkbook(file); } } return(hssfworkbook); }