前端獲取到的Base64字符串格式圖片通常都是通過處理的圖片,例如:裁剪事後的,這裏假設data爲獲取到的Base64字符串格式圖片前端
1 function uploadFile(data) { 2 data = data.split(',')[1] 3 $.ajax({ 4 url: '連接地址', 5 type: 'POST', 6 data: { 'Data': data }, 7 dataType: 'JSON', 8 success: function (data, textStatus) { 9 if (data.Success) { 10 //本身的處理邏輯 11 } 12 else { 13 console,log("失敗"); 14 } 15 }, 16 error: function (XMLHttpRequest, textStatus, errorThrown) { 17 console.log(errorThrown); 18 } 19 }) 20 }
後端Action代碼ajax
1 public JsonResult UploadImage() 2 { 3 try 4 { 5 string base64string = Request["Data"]; 6 byte[] bt = Convert.FromBase64String(base64string); 7 MemoryStream stream = new MemoryStream(bt); 8 Bitmap bitmap = new Bitmap(stream); 9 string tempName = Request.PhysicalApplicationPath + @"\xxxx\" + "b64img_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".png"; 10 bitmap.Save(tempName, ImageFormat.Png); 11 //其餘邏輯 12 13 //返回數據 14 return Json(new {Success = true}) 15 } 16 catch (Exception ex) 17 { 18 Log.Instance.SaveLog(ex.Message);//日誌類本身定義的,能夠忽略 19 } 20 return Json(new {Success = false}); 21 }