C#Base64文件流转PDF文件

4 篇文章 0 订阅

C# Base64文件流转PDF文件

一:需求背景:
我们在获取服务器的PDF或者JPG文件时,代码实现,则需先转Base64文件流,在转成需要的PDF或者JPG文件。

二:获取PDF文件转换Base64(通过共享服务器方式)

 /// <summary>
        /// 获取PDF文件Base64
        /// </summary>
        /// <param name="fileTime">文件月份</param>
        /// <param name="fileName">文件名称</param>
        /// <returns></returns>
        private string GetBase64ReportByPdf(string fileTime, string fileName)
        {
            string pdfBase64 = "";
            fileName = fileName ?? "电子票据";

            try
            {
                string shareUser = "administrator";

                string sharePassword = "123456ABC";
               
                string serverIP = "172.1.1.10";

                string rportFolder = "Data\\ReportData\\reportInfo\\"; //报告目录
                string shareFolder = $"\\\\{serverIP}\\{rportFolder}\\{fileTime}";

                if (Helper.ConnectState(shareFolder, shareUser, sharePassword))  //连接共享服务器
                {
                    pdfBase64 = HeHelper.GetBase64StringByPdf(shareFolder, fileName);
                }
               
            }
            catch (Exception ee)
            {
                throw ee;
            }

            return pdfBase64;
        }

GetBase64StringByPdf方法实现代码如下:

 /// <summary>
        /// 从远程服务器获取文件并转成BASE64
        /// </summary>
        /// <param name="dst">远程服务器路径(共享文件夹路径)</param>
        /// <param name="fileName">远程服务器(共享文件夹)中的文件名称(包含扩展名)</param>
        public static string GetBase64StringByPdf(string dst, string fileName)  //   dst:远程服务器路径    fileName:远程服务器dst路径下的文件名
        {
            byte[] bytes ;
            try
            {
                if (!Directory.Exists(dst))
                {
                    Directory.CreateDirectory(dst);
                }
                dst += $"\\{fileName}.pdf" ;
                FileStream inFileStream = new FileStream(dst, FileMode.Open);
                bytes = new byte[inFileStream.Length];
                inFileStream.Read(bytes, 0, bytes.Length);
                inFileStream.Close();
                return Convert.ToBase64String(bytes);
            }
            catch (DirectoryNotFoundException ex)
            {
                throw ex;
            }

        }

连接共享服务器代码如下:

 /// <summary>
        /// 连接远程共享文件夹
        /// </summary>
        /// <param name="path">远程共享文件夹的路径</param>
        /// <param name="userName">用户名</param>
        /// <param name="passWord">密码</param>
        /// <returns></returns>
        public static bool ConnectState(string path, string userName, string passWord)
        {
            bool Flag = false;
            Process proc = new Process();
            try
            {
                proc.StartInfo.FileName = "cmd.exe";
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardInput = true;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.CreateNoWindow = true;
                proc.Start();
                string dosLine = "net use " + path + " " + passWord + " /user:" + userName;

                //ModuleTxtLog.Write($"pacs报告网络路径", $"pacs报告网络路径{dosLine}");

                proc.StandardInput.WriteLine(dosLine);
                proc.StandardInput.WriteLine("exit");
                while (!proc.HasExited)
                {
                    proc.WaitForExit(1000);
                }
                string errormsg = proc.StandardError.ReadToEnd();
                proc.StandardError.Close();
                if (string.IsNullOrEmpty(errormsg))
                {
                    Flag = true;
                }
                else
                {
                    throw new Exception(errormsg);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                proc.Close();
                proc.Dispose();
            }
            return Flag;
        }

三:Base64文件流转PDF文件

public static void Base64StringConvertPDF(string baseString)
        {
            //如果baseString为空则 读取txt文件

            string filePath = @"F:\base文件\base64.txt";
            string base64BinaryStr = baseString;
            if (string.IsNullOrWhiteSpace(base64BinaryStr))
            {
                FileStream fileStream = new FileStream(filePath, FileMode.Open);
                StreamReader sr = new StreamReader(fileStream);
                base64BinaryStr = sr.ReadLine();
                Console.WriteLine(base64BinaryStr.ToString().Substring(0, 30));
            }
           
            byte[] bytes = Convert.FromBase64String(base64BinaryStr);
            //保存数据到磁盘
            string location = @"E:\电子票据" + DateTime.Now.ToFileTime().ToString() + ".pdf";
            System.IO.FileStream stream = new System.IO.FileStream(location, System.IO.FileMode.CreateNew);
            System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream);
            writer.Write(bytes, 0, bytes.Length);
            writer.Close();
            Console.WriteLine("下载到" + location);

        }

转换后文件如下:

在这里插入图片描述

C# PDF转图片(JPG,Png)推荐看文章:C#PDF转图片

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值