vue 图片下载,下载后端接口返回的blob类型的文件(图片)

参考:https://www.jianshu.com/p/cda41dd5a3df

export function downloadQrcode(params) {
    
    
  return request({
    
    
    url: '/multiplayer-draw/download-qrcode',
    method: 'get',
    // headers: { 'Content-Type': 'image/png; charset=UTF-8' }, //此行可以省略
    responseType: 'blob',		// response类型
    params
  })
}
 downloadQrcode({
    
    id: this.id}).then(res => {
    
    
   var a = document.createElement('a')
   document.body.appendChild(a)
   a.style = 'display: none'
   let blob = new Blob([res], {
    
    
     type: "image/png",
   }); 
   let url = window.URL.createObjectURL(blob); 
   a.href =  url
   a.download = '二维码'
   a.click()
   a.remove()
   window.URL.revokeObjectURL(url) // 释放url
 })

接口返回数据:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_46302247/article/details/130012844