uni-app h5 将base64格式图片下载到本地

//base64str: base64地址("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABNo..."),fileName:文件名("image.png")
export function exportImage(base64str, fileName) {
  // 将base64格式的图片转换成Blob对象
  var arr = base64str.split(","),
    mime = arr[0].match(/:(.*?);/)[1],
    bstr = atob(arr[1]),
    n = bstr.length,
    u8arr = new Uint8Array(n);
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n);
  }
  // 将Blob对象转换成文件并下载到本地
  var blob = new Blob([u8arr], {
    type: mime
  });
  var a = document.createElement('a');
  a.download = fileName;
  a.href = URL.createObjectURL(blob);
  a.click();
}

你可能感兴趣的:(uni-app,javascript,前端)