1)將base64圖片格式轉(zhuǎn)為可讀的url格式
將圖片文件轉(zhuǎn)為二進(jìn)制,然后通過URL的createObjectURL函數(shù),將二進(jìn)制轉(zhuǎn)為url格式文章來源:http://www.zghlxwxcb.cn/news/detail-526451.html
function getBase64URL(pic) {
const blob = base64ImgtoFile(pic)
const blobUrl = window.URL.createObjectURL(blob);
return blobUrl
}
2)將圖片轉(zhuǎn)為文件文章來源地址http://www.zghlxwxcb.cn/news/detail-526451.html
function base64ImgtoFile (dataurl, filename = 'file') {
//將base64格式分割:['data:image/png;base64','XXXX']
const arr = dataurl.split(',')
// .*? 表示匹配任意字符到下一個符合條件的字符 剛好匹配到:
// image/png
const mime = arr[0].match(/:(.*?);/)[1] //image/png
//[image,png] 獲取圖片類型后綴
const suffix = mime.split('/')[1] //png
const bstr = atob(arr[1]) //atob() 方法用于解碼使用 base-64 編碼的字符串
let n = bstr.length
const u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new File([u8arr], `${filename}.${suffix}`, {
type: mime
})
}
到了這里,關(guān)于如何將base64圖片轉(zhuǎn)化為URL格式的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!