//文件全路径: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;
}
/// <summary>
/// Base64字符串转文件并保存
/// </summary>
/// <param name="base64String">base64字符串</param>
/// /// <param name="fileFullPath">保存的文件路径</param>
/// <param name="fileName">保存的文件名</param>
/// <returns>是否转换并保存成功</returns>
public bool Base64StringToFile(string base64String,string fileFullPath, string fileName)
{
bool opResult = false;
try
{
if (!Directory.Exists(fileFullPath))
{
Directory.CreateDirectory(fileFullPath);
}
MemoryStream stream = new MemoryStream(Convert.FromBase64String(base64String));
FileStream fs = new FileStream(fileFullPath + "\\" + fileName, FileMode.OpenOrCreate, FileAccess.Write);
byte[] b = stream.ToArray();
fs.Write(b, 0, b.Length);
fs.Close();
opResult = true;
}
catch (Exception e)
{
}
return opResult;
}