一、Asp.net core 接口接受base64位图片数据保存到服务器
处理方案就是把base64位字符串,转码成二进制数据保存即可。
//上传base64图片
public IActionResult SaveBase64()
{
try
{
//imgdata = SecurityHelper.HTMLDecode(imgdata);
string imgdata = this.HttpContext.Request.Form["imgdata"];
if (imgdata.Contains("base64"))
{
imgdata = imgdata.Split(",")[1];
}
// LogHelper.Write(imgdata);
byte[] byteData = Convert.FromBase64String(imgdata);
MemoryStream ms = new MemoryStream();
ms.Write(byteData, 0, byteData.Length);
Image img = Image.FromStream(ms);
string path = UploadHandle.ServerInfo.SitePath + "/upload/base64/";
if (Directory.Exists(path) == false)
Directory.CreateDirectory(path);
string filename=Guid.NewGuid().ToString() + ".jpg";
path += filename;
string relativeName = "/upload/base64/" + filename;
img.Save( path, ImageFormat.Jpeg);
img.Dispose();
return Json(new
{
relativeName
});
}
catch (Exception ex)
{
return Error(ex);
}
}
更多: