C# Base64用法
public bool Base64StringToFile(string base64String, string fileName)
{
bool opResult = false;
try
{
string strDate = DateTime.Now.ToString("yyyyMMdd");
string fileFullPath = "C:\\WGQ_GROUP_DSMS.SysAdmin\\UpFile\\" + strDate;//文件保存路径
if (!Directory.Exists(fileFullPath))
{
Directory.CreateDirectory(fileFullPath);
}
string strbase64 = base64String.Trim().Substring(base64String.IndexOf(",") + 1); //将‘,’以前的多余字符串删除
MemoryStream stream = new MemoryStream(Convert.FromBase64String(strbase64));
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)
{
logger.Error("异常类型: \t" + e.GetType());
logger.Error("异常描述:\t" + e.Message);
logger.Error("异常方法:\t" + e.TargetSite);
logger.Error("异常堆栈:\t" + e.StackTrace);
}
return opResult;
}