天天看點

base64檔案流轉換為檔案

//将base64流轉換為檔案(圖檔)

string Ipath = ConfigurationManager.AppSettings["WebDirPath"] + dtoPubattach.FILEPATH.TrimStart('/').Replace("/", "\\");

byte[] imageBytes = Convert.FromBase64String(cxmldocImg.SelectSingleNode("//IMAGECONTENT").InnerText);

MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);

ms.Write(imageBytes, 0, imageBytes.Length);

System.Drawing.Image imag = System.Drawing.Image.FromStream(ms, true);

imag.Save(Ipath);

ms.Flush();

ms.Close();

//将base64流轉換為檔案(檔案)

byte[] Pbytes = Convert.FromBase64String(cxmldocPubattach.SelectSingleNode("//FILECONTENT").InnerText);

FileStream fs = new FileStream(Ppath, FileMode.Create);

BinaryWriter bw = new BinaryWriter(fs);

bw.Write(Pbytes);

fs.Flush();

bw.Flush();

bw.Close();

fs.Close();

   

  <summary>

        Base64轉換為圖檔

        </summary>

        <param name="base64String"></param>

        <returns></returns>

        public System.Drawing.Image Base64ToImage(string base64String)

        {

            byte[] imageBytes = Convert.FromBase64String(base64String);

            MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);

            ms.Write(imageBytes, 0, imageBytes.Length);

            System.Drawing.Image imag = System.Drawing.Image.FromStream(ms, true);

            return imag;

        }

        <summary>

        将 byte[] 轉成 Stream

        </summary>

        <param name="bytes"></param>

        <returns></returns>

        public Stream BytesToStream(string path)

        {

            FileStream file = new FileStream(path, FileMode.Create);

            return file;

        }

         <summary>

         将 Stream 寫入檔案

         </summary>

         <param name="stream">Stream</param>

         <param name="fileName">檔案存放路徑</param>

        public void StreamToFile(Stream stream, string fileName)

        {

            把 Stream 轉換成 byte[] 

            byte[] bytes = new byte[stream.Length];

           stream.Read(bytes, 0, bytes.Length);

            // 設定目前流的位置為流的開始 

            stream.Seek(0, SeekOrigin.Begin);

            // 把 byte[] 寫入檔案 

            FileStream fs = new FileStream(fileName, FileMode.Create);

            BinaryWriter bw = new BinaryWriter(fs);

            bw.Write(bytes);

            bw.Close();

            fs.Close();

        }

        public static void BinaryToFile(string fpath, string binary)

        {

            FileStream fs = new FileStream(fpath, FileMode.Create, FileAccess.Write);

            BinaryWriter bw = new BinaryWriter(fs);

            bw.Write(Convert.FromBase64String(binary));

            bw.Close();

            fs.Close();

        }

        public void BytesToStream(byte[] bt, string filename)

        {

            Stream stream = new MemoryStream(bt);

            // 把 Stream 轉換成 byte[] 

            byte[] bytes = new byte[stream.Length];

            stream.Read(bytes, 0, bytes.Length);

            // 設定目前流的位置為流的開始 

            stream.Seek(0, SeekOrigin.Begin);

            // 把 byte[] 寫入檔案 

            FileStream fs = new FileStream(filename, FileMode.Create);

            BinaryWriter bw = new BinaryWriter(fs);

            bw.Write(bytes);

            bw.Close();

            fs.Close();

        }

轉載于:https://www.cnblogs.com/smallstupidwife/archive/2011/09/13/2174623.html