首页 > 技术文章 > 前端小记之vue下载二进制文件

fengxiongmiao 原文

使用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
  })

推荐阅读