vue中的base64圖片轉(zhuǎn)網(wǎng)絡(luò)URL方式
vue base64圖片轉(zhuǎn)網(wǎng)絡(luò)URL
src支持base64圖片,正常base64圖片可以直接復(fù)制到圖片src,也可以將其轉(zhuǎn)為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.length
const u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new File([u8arr], `${filename}.${suffix}`, {
type: mime
})
},打印得到File文件,再轉(zhuǎn)為png圖片

const img = 'data:image/png;base64,' + img this.file = this.base64ImgtoFile(img) // 得到File對(duì)象 this.imgUrl = window.webkitURL.createObjectURL(file) || window.URL.createObjectURL(file) // imgUrl圖片網(wǎng)絡(luò)路徑
base64轉(zhuǎn)url、url轉(zhuǎn)base64
// url轉(zhuǎn)base64
dataURLtoBlob(dataurl) {
return new Promise((resolve, reject) => {
let arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
while(n--){
u8arr[n] = bstr.charCodeAt(n);
}
resolve(new Blob([u8arr], {type:mime}));
})
},
const images = await Promise.all([
this.dataURLtoBlob(this.imageUrl),
this.dataURLtoBlob(this.imageUrl2),
])
// base64轉(zhuǎn)url
dataURLtoBlobURL(dataUrl) {
return new Promise((resolve, reject) => {
let arr = dataUrl.split(','), mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
while(n--) {
u8arr[n] = bstr.charCodeAt(n);
}
let blob = new Blob([u8arr], {type:mime});
let url = URL.createObjectURL(blob);
resolve(url);
});
},
let url1 = await this.dataURLtoBlobURL(this.imageUrl);
let url2 = await this.dataURLtoBlobURL(this.imageUrl2);
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue3學(xué)習(xí)筆記簡(jiǎn)單封裝axios示例實(shí)現(xiàn)
這篇文章主要為大家介紹了vue3學(xué)習(xí)筆記簡(jiǎn)單封裝axios示例實(shí)現(xiàn),2022-06-06
解決vue腳手架項(xiàng)目打包后路由視圖不顯示的問(wèn)題
今天小編就為大家分享一篇解決vue腳手架項(xiàng)目打包后路由視圖不顯示的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
Vue2.0實(shí)現(xiàn)自適應(yīng)分辨率
這篇文章主要為大家詳細(xì)介紹了Vue2.0實(shí)現(xiàn)自適應(yīng)分辨率,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
Vue2(三)實(shí)現(xiàn)子菜單展開(kāi)收縮,帶動(dòng)畫(huà)效果實(shí)現(xiàn)方法
這篇文章主要介紹了vue實(shí)現(xiàn)收縮展開(kāi)效果的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Vue3中導(dǎo)入和使用圖標(biāo)庫(kù)Font Awesome的實(shí)現(xiàn)步驟
Font Awesome 是互聯(lián)網(wǎng)的圖標(biāo)庫(kù)和工具包,被數(shù)百萬(wàn)設(shè)計(jì)師、開(kāi)發(fā)人員和內(nèi)容創(chuàng)建者使用,Font Awesome的圖標(biāo)非常豐富,基本涵蓋你所需要的所有,本文給大家介紹了Vue3中導(dǎo)入和使用圖標(biāo)庫(kù)Font Awesome的具體步驟,需要的朋友可以參考下2025-01-01
Vue3動(dòng)態(tài)路由踩坑實(shí)戰(zhàn)記錄
當(dāng)在開(kāi)發(fā)各種系統(tǒng)應(yīng)用時(shí),我們常常需要根據(jù)不同用戶角色來(lái)展示不同的菜單頁(yè)面,動(dòng)態(tài)路由提供了靈活的路由規(guī)則,可以根據(jù)需要輕松添加、修改或者刪除路由,這篇文章主要介紹了Vue3動(dòng)態(tài)路由踩坑實(shí)戰(zhàn)的相關(guān)資料,需要的朋友可以參考下2026-05-05

