vue——base64图片转网络URL
src支持base64图片,正常base64图片可以直接复制到图片src,也可以将其转为URL
url: 'XXXXXXXX'
imgUrl: ''
base64ImgtoFile (dataurl, filename = 'file') {
const arr = dataurl.split(',')
const mime = arr[0].match(/:(.*?);/)[1]
const suffix = mime.split('/')[1]
const bstr = atob(arr[1])
let n = bstr.length
const u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new File([u8arr], `${filename}.${suffix}`, {
type: mime
})
},
打印得到File文件,再转为png图片

const img = 'data:image/png;base64,' + img
this.file = this.base64ImgtoFile(img)
this.imgUrl = window.webkitURL.createObjectURL(file) || window.URL.createObjectURL(file)