public static FileConversion BuildFileConversion(string originFile, string destinationType) { var res = new FileConversion(); res.Origin = new CSVFile(originFile); switch (destinationType.ToLower()) { case "xml": res.DestinationType = Core.Enums.TypeEnum.XML; break; case "json": res.DestinationType = Core.Enums.TypeEnum.JSON; break; case "csv": res.DestinationType = Core.Enums.TypeEnum.CSV; break; default: break; } return(res); }
public async Task <IActionResult> OnPostUploadAsync([FromForm] IFormFile file) { var fileConversion = new FileConversion(file); var reportId = fileConversion.GetReportId(file.FileName); if (_context.UploadedFiles.Any(o => o.ReportID == reportId)) { throw new Exception("Error: Report Id " + reportId + " already exists in database. Cannot upload duplicate file"); } var uploadedFiles = fileConversion.ConvertFormFilesToUploadedFiles(); foreach (var uploadedFile in uploadedFiles) { _context.UploadedFiles.Add(uploadedFile); await _context.SaveChangesAsync(); } return(Ok()); }
/// <summary> /// Convert the specified file into the specified format /// </summary> /// <param name="fileConversion">Object which contains the origin file and the conversion type</param> /// <returns></returns> public string ConvertFile(FileConversion fileConversion) { string res = string.Empty; var fileData = _dataServiceClient.GetData(fileConversion.Origin.FilePath); string json = fileConversion.Origin.ConvertToJson(fileData); switch (fileConversion.DestinationType) { case Enums.TypeEnum.JSON: res = json; break; case Enums.TypeEnum.XML: res = ConverToXML(json); break; default: break; } return(res); }
private void ExtractChunkFiles(IEnumerable <FileEntryData> files, FileSystemInfo extractDirectory) { foreach (FileEntryData file in files) { byte[] data = file.fileData; if (file.fileLengthData.length != file.fileLengthData.lengthCompressed) { data = FileUtilities.DecompressFile(file.fileData, file.fileLengthData.length); } string[] pathParts = file.fileName.Split(Path.DirectorySeparatorChar); string[] mendedPath = new string[pathParts.Length + 1]; mendedPath[0] = extractDirectory.FullName; for (int i = 0; i < pathParts.Length; i++) { mendedPath[i + 1] = pathParts[i]; } string properPath = Path.Combine(mendedPath); Directory.CreateDirectory(Path.GetDirectoryName(properPath) ?? string.Empty); if (Path.GetExtension(properPath) == ".rawimg") { FileConversion.ConvertRawToPng(data, properPath); } else { FileIO.WriteAllBytes(properPath, data); } // Report that a file has been completed to the progress reporter ProgressReporter.Report(1); } }
private void DoFileUpload() { DirectoryInfo theFolder = new DirectoryInfo(Url); //获取年 string Year = DateTime.Now.Year.ToString(); //获取月 string Month = DateTime.Now.Month.ToString(); //获取日 string Day = DateTime.Now.Day.ToString(); string hm = DateTime.Now.Hour.ToString() + "" + DateTime.Now.Minute.ToString(); string timestamp = Year + Month + Day + hm; string day = DateTime.Now.Day.ToString(); string strDirectory = ConfigurationSettings.AppSettings["SavePath"].ToString(); //存放的路径 固定写死 参见配置文件 string DictoryFileName = Year + @"\" + Month + @"\" + day + @"\"; string LogDirectory = strDirectory + "Log"; //日志文件 //定义名称为当天日期的文件夹的路径 string path = strDirectory + @"\" + DictoryFileName; int NumSuccess = 0; int NumError = 0; int NumAll = FilePaths.Count(); startDate = DateTime.Now;//开始导入数据时间 foreach (FileInfo fi in FilePaths) { //虚拟路径 string tempVirtualPath = fi.FullName.Replace(Url.Remove(Url.LastIndexOf('\\')), string.Empty).Remove(0, 1); at.VirtualPath = tempVirtualPath.Remove(tempVirtualPath.LastIndexOf('\\')); try { bool ConvertResult = false; string destFileName = path + at.VirtualPath + "\\" + fi.Name.Remove(fi.Name.LastIndexOf('.')) + ".pdf"; //创建文件夹 if (!Directory.Exists(path + at.VirtualPath)) { Directory.CreateDirectory(path + at.VirtualPath); } if (".pdf".Contains(fi.Extension)) { try { File.Copy(fi.FullName, destFileName, true); ConvertResult = true; } catch (Exception ex) { ConvertResult = false; } } else if (".doc.docx".Contains(fi.Extension)) { ConvertResult = FileConversion.WordToPDF(fi.FullName, destFileName); } else if (".xls.xlsx".Contains(fi.Extension)) { ConvertResult = FileConversion.ExcelToPDF(fi.FullName, destFileName); } else if (".ppt.pptx".Contains(fi.Extension)) { ConvertResult = FileConversion.PowerPointToPDF(fi.FullName, destFileName); } //判断文件复制、转换是否成功 if (ConvertResult) { //写入数据库 at.Suffix = fi.Extension; at.FileName = fi.Name.Remove(fi.Name.LastIndexOf('.')); at.CreateDate = DateTime.Now; at.BateSize = decimal.Round(decimal.Round(fi.Length) / (1024 * 1024), 2); at.BatchNo = timestamp; at.FtpPath = path.Replace(@"E:\", string.Empty); at.FtpName = fi.Name; at.CreateBy = BaseForm.UserName; at.filetag = "0"; if (new Sino.Bll.AttachmentBLL().Add(at)) { //删除源文件 File.Delete(fi.FullName); //kill进程 KillProcess("WINWORD"); KillProcess("EXCEL"); NumSuccess++; } } else { NumError++; string Text = "【PDF转换失败】:" + fi.FullName + "\r\n【操作时间】:" + DateTime.Now + "\r\n【操作人】:" + BaseForm.UserName + "\r\n==========================================="; WritfLogText(LogDirectory, timestamp, Text); } //更新归档状态和进度条更新 this.Invoke(new Action <int, int>((numSuccess, numError) => { progressBar1.Value = (numSuccess + numError) * 100 / NumAll; label1.Text = "成功导入:" + numSuccess + "个文件\r\n\r\n失败:" + NumError + "个文件"; }), NumSuccess, NumError); } catch { } } //批量加入关键字数据 if (!string.IsNullOrWhiteSpace(this.txtKeyWord.Text.Trim())) { try { DataTable dtAtt = new AttachmentBLL().GetListID(startDate, UserName); DataTable dtEva = new DataTable(); dtEva.Columns.Add("UserName", typeof(string)); dtEva.Columns.Add("OperateTime", typeof(string)); dtEva.Columns.Add("UserName", typeof(string)); dtEva.Columns.Add("Fid", typeof(int)); if (dtAtt != null) { foreach (DataRow row in dtAtt.Rows) { DataRow rowEva = dtEva.NewRow(); rowEva["UserName"] = UserName; rowEva["Fid"] = int.Parse(row["ID"].ToString()); rowEva["KeyWords"] = this.txtKeyWord.Text.Trim();; rowEva["OperateTime"] = DateTime.Now.ToShortDateString(); dtEva.Rows.Add(rowEva); } } new EvaluateBLL().InserDataTable(dtEva, "BT_Evaluate"); } catch { } } this.Invoke(new Action(() => { MessageBox.Show("上传文件成功!", "提示", MessageBoxButtons.OK); })); }