文件与数组(baseBase64)相互转化

文件与数组(baseBase64)相互转化

第一部分 Base64转文件代码


    public void SaveDocument(string path)
    {
        string sFilePath = "服务器文件路径" + "\\path";	//创建路径文件夹
        string sFileName = "文件名字"+ "." + document.imageFormat.ToLower();	//这里的imageFormat就是文件类型
        sFileName = sFilePath + "\\" + sFileName;
        //路径不存在,则创建路径
        if (!Directory.Exists(sFilePath))
        {
            Directory.CreateDirectory(sFilePath);
        }
		//如果文件已经存在,则删除文件
        if (System.IO.File.Exists(sFileName))
        {
            System.IO.File.Delete(sFileName);
        }
      	//注意:文件直接转base64前面会带有“data:application/pdf;base64,”前缀,需要去掉。
		byte[] DocBytes = Convert.FromBase64String(document.content);
		//文件流创建文件内容
        FileStream fs = new FileStream(sFileName, FileMode.CreateNew);
        BinaryWriter bw = new BinaryWriter(fs);
        bw.Write(DocBytes, 0, DocBytes.Length);
        bw.Close();
        fs.Close();
    }

第二部分 本地文件转base64

  //文件全路径:fileName
    public string DocumentToBase64Str(string fileName)
    {
            FileStream filestream = new FileStream(fileName, FileMode.Open);

            byte[] bt = new byte[filestream.Length];
            //调用read读取方法
            filestream.Read(bt, 0, bt.Length);
            string base64Str = Convert.ToBase64String(bt);
            filestream.Close();
            return base64Str;
    }

第三部分 将对象转换为byte数组

public static byte[] Object2Bytes(object obj)
{
    byte[] buff;
    using (MemoryStream ms = new MemoryStream())
    {
        IFormatter iFormatter = new BinaryFormatter();
        iFormatter.Serialize(ms, obj);
        buff = ms.GetBuffer();
    }
    return buff;
}

第四部分 将byte数组转换成对象

public static object Bytes2Object(byte[] buff)
{
    object obj;
    using (MemoryStream ms = new MemoryStream(buff))
    {
        IFormatter iFormatter = new BinaryFormatter();
        obj = iFormatter.Deserialize(ms);
    }
    return obj;
}

以上都是基于本地文件的转换

第五部分 将公网(服务器)转byte[]

第五和第六部分需要引入以下命名空间

using System.Collections.Generic;   //list
using System.Net;   //HttpWebRequest、HttpWebResponse
using System.IO;
        private byte[] GetUrlMemoryStream(string path)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(path);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream responseStream = response.GetResponseStream();

            List<byte> btlst = new List<byte>();
            int b = responseStream.ReadByte();
            while (b > -1)
            {
                btlst.Add((byte)b);
                b = responseStream.ReadByte();
            }
            byte[] bts = btlst.ToArray();
            return bts;
        }

第六部分 将外网的文件转base64 (方法类似于第五部分)

 private byteToBase64 GetUrlMemoryStream(string path)
        {

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(path);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream responseStream = response.GetResponseStream();

            List<byte> btlst = new List<byte>();
            int b = responseStream.ReadByte();
            while (b > -1)
            {
                btlst.Add((byte)b);
                b = responseStream.ReadByte();
            }
            byte[] bts = btlst.ToArray();
            string base64Str = Convert.ToBase64String(bt);
            return base64Str ;
        }

你可能感兴趣的:(C#,asp.net,c#)