首页 > 其他分享 >Base64与File之间的相互转化

Base64与File之间的相互转化

时间:2023-03-10 14:22:05浏览次数:30  
标签:base64ContentData Base64 转化 Length modelItem attachment File new byte

  //文件转base64文件流
 public AttachmentModel ReadDocument(T_BAFJ modelItem) 
        {
            AttachmentModel attachment = new AttachmentModel();
            if (modelItem!=null) {              
                string fileFullPath = Path.Combine(UploadRoot, modelItem.F_WJLJ);//保存文件的全路径             
                using (FileStream filestream = new FileStream($"{fileFullPath}", FileMode.OpenOrCreate, FileAccess.Read))
                {
                    byte[] bt = new byte[filestream.Length];
                    //用来限定每次的读取字节数,也可以byte[] b=new byte[Fsread.Length];             
                    while (true)
                    {
                        int r = filestream.Read(bt, 0, bt.Length);
                        if (r == 0)
                            break;
                    }
                    attachment.Wjlx = modelItem.F_WJLX;
                    attachment.Wjmc = modelItem.F_WJMC;
                    attachment.AttachmentContent = Convert.ToBase64String(bt);
                    //base64StringToPdf(attachment.AttachmentContent, $@"D:\gitProject\规章库\regulationlibrary\guizhangku\Bdyh.bjrd.web\T_BBXM\{modelItem.F_WJMC}");

                }
            }      
            return attachment;
        }
         //文件流转文件
        public void base64StringToPdf(String base64Content, String filePath)
        {
            try
            {        
//注意:文件直接转base64前面会带有“data:application/pdf;base64,”前缀,需要去掉。 base64Content = base64Content.Replace("data:application/pdf;filename=generated.pdf;base64,", string.Empty); string base64ContentData = base64Content.Trim().Replace("%", "").Replace(",", "").Replace(" ", "+"); if (base64ContentData.Length % 4 > 0) { base64ContentData = base64ContentData.PadRight(base64ContentData.Length + 4 - base64ContentData.Length % 4, '='); } byte[] bytes = Convert.FromBase64String(base64ContentData); string location = filePath; System.IO.FileStream stream = new FileStream(location, FileMode.CreateNew); System.IO.BinaryWriter writer = new BinaryWriter(stream); writer.Write(bytes, 0, bytes.Length); writer.Close(); } catch (Exception ex) { //LogHelper.WriteLog(GetType(), "base64StringToPdf异常错误为:" + ex.Message); } }

 

标签:base64ContentData,Base64,转化,Length,modelItem,attachment,File,new,byte
From: https://www.cnblogs.com/DSC1991/p/17203210.html

相关文章

  • Makefile 规则
    1.make后面不带参数默认执行的是第一个目标,而不是default,如下:$make===test===$catMakefiletest:@echo"===test==="default:@echo"===defaul......
  • 字节输入流_FileInputStream
    publicstaticvoidmain(String[]args){//字节流的读操作FileInputStreamf=null;try{//注意:读取数据的时候,如果文件......
  • C# HttpPost 【ContentType:multipart/form-data】表单提交 file 类型数据方法 2
    参考来源:https://blog.csdn.net/qq_39788123/article/details/128495546 try{Dictionary<string,string>headerDict=newDictiona......
  • 前端中文转base64
    btoa浏览器的方法,可将字符串编码为Base64编码的ASCII字符串。btoa("helloworld");//'aGVsbG8gd29ybGQ='UTF-16字符编码表示不了中文,可以先把中文编码为Uint8Ar......
  • java-IO-File类概述和构造方法
         ......
  • cdc-file-transfer 谷歌开源的windows 到linux 同步工具
    cdc-file-transfer是基于contentdefinedchunking以及fastcdc技术,cdc-file-transfer目前提供了两种工具cdc_rsync类似rsync的同步能力,进行文件拷贝,但是性能相比rsync......
  • File的获取和判断
    publicstaticvoidmain(String[]args)throwsIOException{Filef=newFile("C:\\a.txt");//isFile()方法测试此抽象路径名表示的File是否为......
  • Makefile编程基础
    一个工程中的源文件不计其数,其按类型、功能、模块分别放在若干个目录中,makefile定义了一系列的规则来指定哪些文件需要先编译,哪些文件需要后编译,哪些文件需要重新编译,甚至......
  • vue中将base64流数据转成pdf文件可打印
    vue中将base64流数据转成pdf文件可打印这次后端返参不是oss那边传回来的,而是传给我一串base64编码的字符串,让我去生成可打印的pdf,返参如下所示。一、base编码转为pdf方......
  • File的删除
    publicstaticvoidmain(String[]args)throwsIOException{Filef=newFile("C:\\a\\b");//删除文件夹注意事项:1、删除文件是直接物理删除,......