分享

文件转化成二进制以及base64编码字符串返回

 实力决定地位 2016-12-21
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
 
namespace WooBase.Utility
{
    /// <summary>
    /// 文件转化成二进制工具类
    /// </summary>
    public class FileBinaryConvertHelper
    {
            /// <summary>
            /// 将文件转换为byte数组
            /// </summary>
            /// <param name="path">文件地址</param>
            /// <returns>转换后的byte数组</returns>
            public static byte[] File2Bytes(string path)
            {
                if (path.IndexOf('~') < 0)
                {
                    if (HttpContext.Current == null)
                    {
                        path = AppDomain.CurrentDomain.BaseDirectory + "/" + path;
 
                    }
                    else
                    {
                        path = HttpContext.Current.Server.MapPath("~/" + path);
                    }
                }
                if (!System.IO.File.Exists(path))
                {
                    return new byte[0];
                }
               
                FileInfo fi = new FileInfo(path);
                byte[] buff = new byte[fi.Length];
 
                FileStream fs = fi.OpenRead();
                fs.Read(buff, 0, Convert.ToInt32(fs.Length));
                fs.Close();
 
                return buff;
            }
            /// <summary>
            /// 将文件转换为byte数组
            /// </summary>
            /// <param name="path">文件地址</param>
            /// <returns>转换后的byte数组</returns>
            public static string File2BytesBase64strring(string path)
            {
                if (path.IndexOf('~') < 0)
                {
                    if (HttpContext.Current == null)
                    {
                        path = AppDomain.CurrentDomain.BaseDirectory + "/" + path;
 
                    }
                    else
                    {
                        path = HttpContext.Current.Server.MapPath("~/" + path);
                    }
                }
                if (!System.IO.File.Exists(path))
                {
                    return string.Empty;
                }
               
                FileInfo fi = new FileInfo(path);
                byte[] buff = new byte[fi.Length];
 
                FileStream fs = fi.OpenRead();
                fs.Read(buff, 0, Convert.ToInt32(fs.Length));
                fs.Close();
 
                return Convert.ToBase64String(buff);
            }
 
            /// <summary>
            /// 将byte数组转换为文件并保存到指定地址
            /// </summary>
            /// <param name="buff">byte数组</param>
            /// <param name="savepath">保存地址</param>
            public static void Bytes2File(byte[] buff, string savepath)
            {
                if (System.IO.File.Exists(savepath))
                {
                    System.IO.File.Delete(savepath);
                }
 
                FileStream fs = new FileStream(savepath, FileMode.CreateNew);
                BinaryWriter bw = new BinaryWriter(fs);
                bw.Write(buff, 0, buff.Length);
                bw.Close();
                fs.Close();
            }
        }
 
    }
 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约