js 前端不調接口直接下載圖片


// 下載圖片 
      downPhoto (path) {
        this.downloadFiles(path)
      },
      // 下載
      downloadFiles (content) {
        console.log(content)
        const downloadElement = document.createElement('a')  // 創建下載的鏈接
        let blob = this.base64ToBlob(content) // new Blob([content]);
        const href = window.URL.createObjectURL(blob)
        downloadElement.href = href
        downloadElement.download = '參保人相片.jpg'  // 下載后的文件名
        document.body.appendChild(downloadElement)
        downloadElement.click() // 下載
        document.body.removeChild(downloadElement) // 下載完成 移除 a
        window.URL.revokeObjectURL(href) // 釋放blob對象
      },
      // base64轉blob
      base64ToBlob(code) {
        let parts = code.split(';base64,')
        let contentType = parts[0].split(':')[1]
        let raw = window.atob(parts[1])
        let rawLength = raw.length
        let uInt8Array = new Uint8Array(rawLength)
        for (let i = 0; i < rawLength; ++i) {
          uInt8Array[i] = raw.charCodeAt(i)
        }
        return new Blob([uInt8Array], {type: contentType})
      },
<button  class="cancel-btn cursor" @click="downPhoto(dataDetail.photo64Byte)">下載圖片</button>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM