c#64进制转任何文件到目标地址
/// <summary>
/// base64格式转换为任何格式文件保存
/// </summary>
/// <param name="source">64进制流</param>
/// <param name="name">有名称则传名称,没有则传空值按时间生成</param>
/// <param name="type">如"mp3"</param>
public static string Base64ToFile(string source, string name, string type)
{
try
{
string LogPath = HttpContext.Current.Server.MapPath("/upload/" + type + "/");
if (!Directory.Exists(LogPath))
{
Directory.CreateDirectory(LogPath);
}
byte[] bytes = Convert.FromBase64String(source);
var path = "";
if (name == "")
{
path = DateTime.Now.ToFileTime() + "." + type;
}
else
{
path = name + "." + type;
}
string localPath = AppDomain.CurrentDomain.BaseDirectory + "upload\\" + type + "\\" + path;
using (var fs = new FileStream(localPath, FileMode.Create))
{
fs.Write(bytes, 0, bytes.Length);
fs.Flush();
}
return "upload/" + type + "/" + path;
}
catch (Exception e)
{
throw (e);
}
}