请求二进制流文件,并转码下载

 
this.$http({
url: this.exportUrl,
data: this.useSearch,
responseType: 'blob'
})
.then((res) => {
const blob = new Blob([res.data], { type: "application/x-xls" })
// for IE
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(blob, this.downloadName || '表格.xls');
} else {
// 兼容不同浏览器的URL对象
const url = window.URL || window.webkitURL || window.moxURL
// 创建下载链接
const downloadHref = url.createObjectURL(blob)
// 创建a标签并为其添加属性
let downloadLink = document.createElement('a')
downloadLink.href = downloadHref
downloadLink.download = this.downloadName || '表格.xls';
// 触发点击事件执行下载
downloadLink.click()
}
})
.finally(() => {
 
})
posted @ 2019-03-29 09:46  孤魂1715  阅读(204)  评论(0编辑  收藏  举报