前置条件:后台接口返回二进制流文件

一、设置前端请求的的

  

responseType: 'blob' 

 

二、接收请求数据并调用下载

var content = res.data // 接口返回的二进制流
var filename = fileName.xls // 文件名,根据需要更改
var blob = new Blob([content], {type: 'application/vnd.ms-excel'}) // 转化为blob对象 if (window.navigator.msSaveOrOpenBlob) { // IE navigator.msSaveBlob(blob, filename) } else { var aTag = document.createElement('a') aTag.download = filename aTag.href = URL.createObjectURL(blob) aTag.click() URL.revokeObjectURL(blob) }

  

var blob = new Blob([content], {type: 'application/vnd.ms-excel'})
if (window.navigator.msSaveOrOpenBlob) {
navigator.msSaveBlob(blob, filename)
} else {
var aTag = document.createElement('a')
aTag.download = filename
aTag.href = URL.createObjectURL(blob)
aTag.click()
URL.revokeObjectURL(blob)
}
posted on 2019-05-15 15:02  百科全输  阅读(751)  评论(0编辑  收藏  举报