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

exportExcel () {
var str = '?'
for (var i in this.search) {
str += i + '=' + (this.search[i] || '')
}
this.loading.start()
this.$http({
url: this.exportUrl,
method: 'get',
params: this.search,
responseType: 'blob' //转码
})
.then((res) => {
const blob = new Blob([res.data], { type: "application/x-xls" })
// 兼容不同浏览器的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 = '项目回款情况差异表.xls';
// 触发点击事件执行下载
downloadLink.click()
})
.finally(() => {
this.loading.stop()
})
}

转载于:https://www.cnblogs.com/gudun/p/10619344.html

你可能感兴趣的:(请求二进制流文件,并转码下载)