接口代码:
export function getDownloadAPI(url) {
return request({
url: url,
responseType: 'blob',
method: 'GET'
})
}
注意,以上代码因为pdf的下载地址后台已经返回,所以这里的url需要用传参的方式!!
html代码:
<el-button @click="hrefClick" type="success" >下载</el-button>
js代码:
// pdf下载
hrefClick(){
getDownloadAPI(this.formInline.invoice_url).then(res => {
if(res.size > 0){
const content = res
const blob = new Blob([content])
const fileName = '发票.pdf'
if ('download' in document.createElement('a')) { // 非IE下载
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)
} else { // IE10+下载
navigator.msSaveBlob(blob, fileName)
}
/* this.$message.success("导出成功")*/
}
})
},