//public static response RestoreFileFromDb(Models.DocumentScanat ds) public static response RestoreFileFromDb(dynamic ds) { try { if (ds.FILE_CONTENT == null) { ds.GetFileContent(); } if (!File.Exists(Path.Combine(CommonFunctions.GetScansFolder(), ds.CALE_FISIER))) { FileStream fs = new FileStream(Path.Combine(CommonFunctions.GetScansFolder(), ds.CALE_FISIER), FileMode.CreateNew, FileAccess.ReadWrite); fs.Write(ds.FILE_CONTENT, 0, ds.FILE_CONTENT.Length); fs.Flush(); fs.Dispose(); } try { string outputThumbFile = ds.CALE_FISIER.Replace(ds.EXTENSIE_FISIER, "_Custom.jpg"); if (!File.Exists(Path.Combine(CommonFunctions.GetScansFolder(), outputThumbFile))) { ThumbNails.GenerateImgThumbNail(CommonFunctions.GetThumbNailSizes(ThumbNailType.Custom), ds.CALE_FISIER); } } catch (Exception exp) { LogWriter.Log(exp); } return(new response(true, "", null, null, null)); // to do - restore thumbnails }catch (Exception exp) { LogWriter.Log(exp); return(new response(false, exp.Message, null, null, new List <Error>() { new Error(exp) })); } }
public static byte[] UploadFile(string filePath, string fileName) { FileStream fs = File.Open(Path.Combine(filePath, fileName), FileMode.Open, FileAccess.Read); byte[] toReturn = new byte[fs.Length]; fs.Read(toReturn, 0, (int)fs.Length); fs.Flush(); fs.Dispose(); response r = ThumbNails.GenerateImgThumbNail(filePath, fileName); return(toReturn); }
/* * public static Dictionary<string, byte[]> UploadFile(ICollection<IFormFile> files) * { * Dictionary<string, byte[]> toReturn = new Dictionary<string, byte[]>(); * * foreach (IFormFile file in files) * { * if (file.Length > 0) * { * toReturn.Add(file.FileName, UploadFile(file)); * } * } * return toReturn; * } */ /* * public static byte[] UploadFile(IFormFile file) * { * BinaryReader reader = new BinaryReader(file.OpenReadStream()); * byte[] toReturn = reader.ReadBytes((int)file.Length); * return toReturn; * } */ public static byte[] UploadFile(string filePath) { string newFilePath = File.Exists(filePath) ? filePath : Path.Combine(CommonFunctions.GetScansFolder(), filePath); FileStream fs = File.Open(newFilePath, FileMode.Open, FileAccess.Read); byte[] toReturn = new byte[fs.Length]; fs.Read(toReturn, 0, (int)fs.Length); fs.Flush(); fs.Dispose(); response r = ThumbNails.GenerateImgThumbNail(filePath); return(toReturn); }
public static string SaveBinaryContentToFile(byte[] fileContent, string extension) { string filePath = Guid.NewGuid() + extension; string newFilePath = File.Exists(filePath) ? filePath : Path.Combine(CommonFunctions.GetScansFolder(), filePath); FileStream fs = File.Create(newFilePath); fs.Write(fileContent, 0, fileContent.Length); fs.Flush(); fs.Dispose(); response r = ThumbNails.GenerateImgThumbNail(filePath); return(filePath); }
public static response ExportDocumenteDosarToPdf(Models.Dosar dosar) { try { PdfFixedDocument poDocument = new PdfFixedDocument(); Models.DocumentScanat[] ds = (Models.DocumentScanat[])dosar.GetDocumente().Result; foreach (Models.DocumentScanat dsj in ds) { try { if (dsj.VIZA_CASCO) { //MemoryStream ms = new MemoryStream(dsj.FILE_CONTENT); // -- pt. citire content fisier din BD FileStream ms = File.Open(Path.Combine(CommonFunctions.GetScansFolder(), dsj.CALE_FISIER), FileMode.Open, FileAccess.Read); switch (dsj.EXTENSIE_FISIER.Replace(".", "").ToLower()) { case "pdf": PdfFixedDocument pd = new PdfFixedDocument(ms); for (int i = 0; i < pd.Pages.Count; i++) { poDocument.Pages.Add(pd.Pages[i]); } break; case "png": Xfinium.Pdf.Graphics.PdfPngImage pngImg = new Xfinium.Pdf.Graphics.PdfPngImage(ms); PdfPage p = new PdfPage(); ThumbNailSize tns = ThumbNails.ScaleImage(pngImg, p.Width - PDF_PAGE_MARGIN, p.Height - PDF_PAGE_MARGIN); p.Graphics.DrawImage(pngImg, (p.Width - tns.Width) / 2, (p.Height - tns.Height) / 2, tns.Width, tns.Height); //p.Graphics.DrawImage(pngImg, 0, 0, p.Width, p.Height); poDocument.Pages.Add(p); break; case "jpg": case "jpeg": Xfinium.Pdf.Graphics.PdfJpegImage jpgImg = new Xfinium.Pdf.Graphics.PdfJpegImage(ms); p = new PdfPage(); tns = ThumbNails.ScaleImage(jpgImg, p.Width - PDF_PAGE_MARGIN, p.Height - PDF_PAGE_MARGIN); p.Graphics.DrawImage(jpgImg, (p.Width - tns.Width) / 2, (p.Height - tns.Height) / 2, tns.Width, tns.Height); //p.Graphics.DrawImage(jpgImg, 0, 0, p.Width, p.Height); poDocument.Pages.Add(p); break; case "tiff": Xfinium.Pdf.Graphics.PdfTiffImage tiffImg = new Xfinium.Pdf.Graphics.PdfTiffImage(ms); p = new PdfPage(); tns = ThumbNails.ScaleImage(tiffImg, p.Width - PDF_PAGE_MARGIN, p.Height - PDF_PAGE_MARGIN); p.Graphics.DrawImage(tiffImg, (p.Width - tns.Width) / 2, (p.Height - tns.Height) / 2, tns.Width, tns.Height); //p.Graphics.DrawImage(tiffImg, 0, 0, p.Width, p.Height); poDocument.Pages.Add(p); break; default: ms.Flush(); ms.Dispose(); throw new Exception("unsupportedFormat"); } ms.Flush(); ms.Dispose(); } } catch (Exception exp) { LogWriter.Log(exp); } } if (poDocument.Pages.Count > 0) { string fileName = dosar.NR_DOSAR_CASCO.Replace('/', '_').Replace(' ', '_') + "_documente.pdf"; FileStream fs = File.Open(Path.Combine(CommonFunctions.GetPdfsFolder(), fileName), FileMode.Create, FileAccess.ReadWrite); poDocument.Save(fs); fs.Flush(); fs.Dispose(); return(new response(true, Path.Combine(CommonFunctions.GetPdfsFolder(), fileName), Path.Combine(CommonFunctions.GetPdfsFolder(), fileName), null, null)); } else { return(new response(false, ErrorParser.ErrorMessage("dosarFaraDocumente").ERROR_MESSAGE, null, null, new List <Error>() { ErrorParser.ErrorMessage("dosarFaraDocumente") })); } } catch (Exception exp) { LogWriter.Log(exp); return(new response(false, exp.Message, null, null, new System.Collections.Generic.List <Error>() { new Error(exp) })); } }