文件与base64二进制转换

/// <summary>
/// 文件转换为Base64二进制流
/// </summary>
/// <param name="FilePath"></param>
/// <returns></returns>
public static string FileToBase64(string FilePath)
{
  FileStream fileStream = File.Open(FilePath, FileMode.OpenOrCreate);
  byte[] buffer = new byte[fileStream.Length];
  fileStream.Read(buffer, 0, buffer.Length);
  fileStream.Close();
  return Convert.ToBase64String(buffer);
}

        
/// <summary>
/// Base64二进制流还原文件
/// </summary>
/// <param name="FilePath">存放文件的路径</param>
/// <param name="StringBase64">文件Base64二进制流</param>
public static void Base64TeCode(string FilePath,string StringBase64)
{
  FileStream fileStream = new FileStream(FilePath, FileMode.Create);
  byte[] buffer = Convert.FromBase64String(StringBase64);
  fileStream.Write(buffer, 0, buffer.Length);
  fileStream.Close();
}

 

posted @ 2016-07-20 14:06  有思想的码农  阅读(12289)  评论(0编辑  收藏  举报