前端小记之vue下载二进制文件

使用bold对象生成一个url对象来作为a标签下载的文件

const elink = document.createElement('a') // 创建a标签
elink.style.display = 'none'
const blob = new Blob([res]) // 创建blob对象
elink.download = '模板.xlsx'
elink.href = URL.createObjectURL(blob) // 创建一个url对象
document.body.appendChild(elink)
elink.click();
URL.revokeObjectURL(elink.href) // 释放URL 对象
document.body.removeChild(elink)
 request({
    url:'http://xxxxxx',
    method:'get',
    params:{query},
    responseType: 'blob'  // 请求加上responseType为blob
  })

posted on 2021-06-04 16:07  fengxiongmiao  阅读(438)  评论(0编辑  收藏  举报

导航