本人实际工作中记录的,不喜勿碰
特别注意:
1.请求接口设置请求类型----responseType: "blob"
2.下载文件名的后缀根据需求修改
3.tyep:根据实际需求更改。
例如:type: "application/vnd.ms-excel"或者type: "application/octet-stream;charset=UTF-8",
实现主要代码如下:
let blob = new Blob([res.data], {
type: "application/vnd.ms-excel",
});
const fileName = '测试' + '.xlsx' // 下载文件名称
const elink = document.createElement('a')
elink.download = fileName
elink.style.display = 'none'
elink.href = URL.createObjectURL(blob)
document.body.appendChild(elink)
elink.click()
URL.revokeObjectURL(elink.href) // 释放URL 对象
document.body.removeChild(elink)
})