html
script
后台api
把圖片base64存在數據庫的二進制類型表中,記得把圖片的后綴存上
[HttpPost]
public ActionResult FileData(Microsoft.AspNetCore.Http.IFormFile? file)
{
//數據庫
Qrcode modellist = new Qrcode();
//獲取后綴
var Exttype = file.ContentType.Replace("image/", ".");
Stream fs = file.OpenReadStream();
BinaryReader br = new BinaryReader(fs);
byte[] imgBytesIn = br.ReadBytes((int)fs.Length);
modellist.Image = imgBytesIn; //數據庫的Image的類型是二進制
modellist.Ext = Exttype;//圖片后綴
return new JsonResult(gzhContext.SaveChanges());
}
展示圖片
先把圖片二進制轉base64
string img = Convert.ToBase64String(數據庫中取到二進制);
var erjinzhi = "data:" + 數據庫中存的圖片后綴+ ";base64," + img;
把 erjinzhi 傳到前台賦到 <Img src = "erjinzhi">就可以顯示了