vue 后端返回base64轉(zhuǎn)成圖片并下載

//qrBase64是后臺傳回來的base64數(shù)據(jù)
handleDownloadQrIMg(qrBase64) {
      // 這里是獲取到的圖片base64編碼,這里只是個例子哈,要自行編碼圖片替換這里才能測試看到效果
      const imgUrl = `data:image/png;base64,${qrBase64}`
      // 如果瀏覽器支持msSaveOrOpenBlob方法(也就是使用IE瀏覽器的時候),那么調(diào)用該方法去下載圖片
      if (window.navigator.msSaveOrOpenBlob) {
        const bstr = atob(imgUrl.split(',')[1])
        let n = bstr.length
        const u8arr = new Uint8Array(n)
        while (n--) {
          u8arr[n] = bstr.charCodeAt(n)
        }
        const blob = new Blob([u8arr])
        window.navigator.msSaveOrOpenBlob(blob, 'chart-download' + '.' + 'png')
      } else {
        // 這里就按照chrome等新版瀏覽器來處理
        const a = document.createElement('a')
        a.href = imgUrl
        a.setAttribute('download', 'chart-download')
        a.click()
      }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。