本文主要是介绍vue实现base64图片转网络URL,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
src支持base64图片,正常base64图片可以直接复制到图片src,也可以将其转为URL
// data
url: 'XXXXXXXX' // base64编码
imgUrl: '' // 图片路径// methods
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.lengthconst u8arr = new Uint8Array(n)while (n--) {u8arr[n] = bstr.charCodeAt(n)}return new File([u8arr], `${filename}.${suffix}`, {type: mime})},
const img = 'data:image/png;base64,' + img
this.file = this.base64ImgtoFile(img) // 得到File对象
this.imgUrl = window.webkitURL.createObjectURL(file) || window.URL.createObjectURL(file)
这篇关于vue实现base64图片转网络URL的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!