base64字符串是包含文件格式的文件字符串,例如:data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAgAElEQVR4Xsy9+……
根据base64字符串可以转化成真实的文件。思路是,首先将base64字符串读取城字节数组;然后将字节数组读入到字节流(内存流);其次将base64字符串开头部分包含的文件格式提取出来,得到文件类型并转换成相应后缀名;最后,根据字节流和文件类型,即可转化成相应的文件,并保存到物理磁盘。
特别是最近玩AngularJS时候,用到图片上传,裁剪图片保存功能,需要用到ngImgCrop裁剪图片控件。它就是将图片裁剪后保存成base64字符串结果的。
示例:base64编码的文本转为图片
/// <summary>
/// base64编码的文本转为图片
/// </summary>
/// <param name="base64"></param>
/// <returns></returns>
private static Image Base64StringToImage(string base64)
{
byte[] bytes = Convert.FromBase64String(base64);
using (MemoryStream ms = new MemoryStream(bytes))
{
Bitmap bmp = new Bitmap(ms);
return bmp;
}
}
示例:获取base64字符串开头部分包含的文件格式
var m = new Regex(@"(?<=data\:)[\w\/_\.]+(?=\;)", RegexOptions.IgnoreCase).Match(byteStr);
string ContentType = null != m ? m.Value : "image/png";
示例:获取文件大小
/// <summary>
/// 获取文件/文件夹大小(字节)
/// </summary>
/// <param name="filePath">路径</param>
/// <returns></returns>
private static long FileSize(string filePath)
{
long temp = 0;
//路径是否是文件
if (File.Exists(filePath) == false)
{
//文件目录
string[] entries = Directory.GetFileSystemEntries(filePath);
foreach (string item in entries)
{
//遍历文件目录内的所有子文件目录,子文件
temp += FileSize(item);
}
}
else
{
FileInfo fileInfo = new FileInfo(filePath);
return fileInfo.Length; //获取真实文件的大小(字节)
}
return temp;
}
全部代码:
public class FileUploadResultModel
{
public Guid FileID { get; set; }
public long FileSize { get; set; }
} public static class Base64FileHandle
{
/// <summary>
/// base64编码的文本转为图片
/// </summary>
/// <param name="base64"></param>
/// <returns></returns>
private static Image Base64StringToImage(string base64)
{
byte[] bytes = Convert.FromBase64String(base64);
using (MemoryStream ms = new MemoryStream(bytes))
{
Bitmap bmp = new Bitmap(ms);
return bmp;
}
}
/// <summary>
/// 获取文件/文件夹大小(字节)
/// </summary>
/// <param name="filePath">路径</param>
/// <returns></returns>
private static long FileSize(string filePath)
{
long temp = ;
if (File.Exists(filePath) == false)
{
string[] entries = Directory.GetFileSystemEntries(filePath);
foreach (string item in entries)
{
temp += FileSize(item);
}
}
else
{
FileInfo fileInfo = new FileInfo(filePath);
return fileInfo.Length;
}
return temp;
}
private static string SuffixName(string ContentType)
{
switch (ContentType.Trim().ToLower())
{
case "image/jpg":
case "image/jpeg":
return ".jpg"; case "image/gif":
return ".gif";
case "image/bmp":
return ".bmp";
case "image/png":
return ".png";
default:
return "";
}
} /// <summary>
/// 创建圈子
/// </summary>
/// <param name="community"></param>
/// <returns></returns>
public static ResponseModel<FileUploadResultModel> ngImgCropHandle(string base64)
{
var response = new ResponseModel<FileUploadResultModel>() { IsLogin = true }; string byteStr = base64.Trim();//data:image/png;base64,
int delLength = byteStr.IndexOf(',') + ;
string str = byteStr.Substring(delLength, byteStr.Length - delLength);
Image returnImage = Base64StringToImage(str); var m = new Regex(@"(?<=data\:)[\w\/_\.]+(?=\;)", RegexOptions.IgnoreCase).Match(byteStr);
string ContentType = null != m ? m.Value : "image/png"; Guid filename = Guid.NewGuid();
string filepath = @"D:\wwwroot\" + filename + SuffixName(ContentType);
returnImage.Save(filepath); response.Data = new FileUploadResultModel() { FileID = filename, FileSize = FileSize(filepath) };
response.Success = true;
response.Message = "ok";
return response;
} }