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();
        }

posted @ 2011-09-13 13:33  (*^__^*) 嘻嘻…  阅读(1989)  评论(0编辑  收藏  举报