C#将文件转成16进制码流写入数据库存起来,访问的时候再还原成PDF文件
来源:互联网 发布:大数据掌握的技术 编辑:程序博客网 时间:2024/06/08 09:27
C#将文件转成16进制码流写入数据库存起来,访问的时候再还原成PDF文件。
适合将文件写入数据库,远程访问的时候还原
- 首先把文件转成十六进制文件流
public void FileToStream() { try { IPdfClassBll pdfClassBll = DataFactory.GetPdfClass(); FileStream fs = new FileStream(@"文件路径/***.PDF", FileMode.Open); BinaryReader br = new BinaryReader(fs); Byte[] byData = br.ReadBytes((int)fs.Length); StringBuilder strResult = new StringBuilder(byData.Length * 8); string binary = byteToHexStr(byData); PdfInfo pdfinfo = new PdfInfo(); pdfinfo.PRTNUM = "PH001";//创建的新文件名 pdfinfo.PDFREPORT = binary; pdfClassBll.Add(pdfinfo); fs.Close(); br.Close(); } catch (Exception ex) { MessageBox.Show("文件写入失败~!"); } } /// <summary> /// 字节数组转16进制字符串 /// </summary> /// <param name="bytes"></param> /// <returns></returns> public static string byteToHexStr(byte[] bytes) { string returnStr = ""; if (bytes != null) { for (int i = 0; i < bytes.Length; i++) { returnStr += bytes[i].ToString("X2"); } } return returnStr; } /// <summary>
2.读取数据库中的 PDF流 信息,并写入文件======================= public void StreamToFile(string prtnum) { try { //写入位置 SampleConfig config = MonitorConfigManager.GetInstance().GetSampleConfig(); string reportPath = config.ReportPath; reportPath = reportPath + "\\" + oSampleInfo.PRTNUM + ".pdf"; IPdfClassBll pdfClassBll = DataFactory.GetPdfClass(); PdfInfo pdfInfo = pdfClassBll.GetPdfReportByPRTNUM(prtnum); string strPdf = pdfInfo.PDFREPORT; byte[] bytes = strToToHexByte(strPdf); FileStream fileStream = new FileStream(reportPath, FileMode.OpenOrCreate, FileAccess.Write); fileStream.Write(bytes, 0, bytes.Length); BinaryWriter binaryWriter = new BinaryWriter(fileStream); binaryWriter.Write(bytes); binaryWriter.Close(); fileStream.Close(); } catch (Exception ex) { } } /// <summary> /// 16进制字符串转为文字 /// </summary> /// <param name="hexString"></param> /// <returns></returns> private static byte[] strToToHexByte(string hexString) { hexString = hexString.Replace(" ", ""); if ((hexString.Length % 2) != 0) hexString += " "; byte[] returnBytes = new byte[hexString.Length / 2]; for (int i = 0; i < returnBytes.Length; i++) returnBytes[i] = Convert.ToByte(hexStringSubstring(i * 2, 2), 16); return returnBytes; }
## 欢迎大家学习,请尊重原创,转载注明出处 ##
1 0
- C#将文件转成16进制码流写入数据库存起来,访问的时候再还原成PDF文件
- 如何将文件转成PDF
- 用C#将数据写入到Excel文件的方法
- 将数据写入文件
- 将数据写入文件
- 怎么将编辑好的PDF文件转成PPT
- 将ppt转成pdf文件的常见方法汇总
- 能将pdf文件转成ppt吗
- Ubuntu下将文本文件转成PDF文件
- Ubuntu下将文本文件转成PDF文件
- iava 实现将office 文件转成pdf
- 将图像数据写入文件的函数
- 将html的表单数据写入文件
- 将Markdown文件转成word文件和PDF文件
- 怎么用PDF转换器将PDF文件转成txt
- 作业二:将字符串数组把重复数据剔除后写入一个文件保存起来。
- C# 将数据写入文件并换行显示
- C#生成txt文件,并将数据写入进去
- js页面跳转整理
- 前台登录账号改变时迅速清空记录的登录密码
- iOS中NSString自适应宽高
- 八种排序算法
- Windows下使用DOS命令进入MySQL数据库
- C#将文件转成16进制码流写入数据库存起来,访问的时候再还原成PDF文件
- he method setAttribute(String,object)in the type HttpSession is not appl
- Codeforces Round #382 (Div. 2) C
- EF框架(一)搭建过程
- Unity3d与Android交互总结
- java中length,length(),size()区别
- android Studio中preview 的显示、固定与隐藏
- 802.1P和IP Precedence及DSCP优先级的分类和对应
- 实现字符串右循环移位函数