图片 base64转byte[]
/// <summary>
/// 保存base64图片,返回阿里云地址
/// </summary>
/// <param name="imgCode"></param>
/// <returns></returns>
private string SaveBase64Image(string imgCode)
{
string imgUrl = string.Empty;
if (!string.IsNullOrEmpty(imgCode))
{
Regex reg = new Regex(@"data:(image.+);base64,(.+)");
if (reg.IsMatch(imgCode))
{
var matchs = reg.Match(imgCode);
string contentType = matchs.Groups[1].Value;
string base64Code = matchs.Groups[2].Value;
string extendType = contentType.Replace("image/", "");
byte[] arr = Convert.FromBase64String(base64Code);//base64转byte[]
//var picture = _pictureService.InsertPicture(arr, extendType);
}
}
return imgUrl;
}
Stream转byte[]
/// <summary>
/// StreamToBytes
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
public static byte[] StreamToBytes(Stream stream)
{
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
// 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin);
return bytes;
}
byte[]转base64
var base64Url = Convert.ToBase64String(avatarData);//avatarData为byte[]
base64转byte[]
byte[] data = Convert.FromBase64String(signedString);
file转base64
var requestFiles = Request.Files;
if (requestFiles.Count > 0)
{
var avatarData = CommonHelper.StreamToBytes(requestFiles[0].InputStream);//Stream转byte[]
var base64Url = Convert.ToBase64String(avatarData);//byte[]转base64
}