loadDown (val) { // 点击下载
this.fileLinkToStreamDownload(this.url, this.fileName)
},
fileLinkToStreamDownload (url, fileName) {
const xhr = new XMLHttpRequest()
xhr.open('get', url, true)
xhr.setRequestHeader('Content-Type', `application/pdf`)
xhr.responseType = 'blob'
const that = this
xhr.onload = function () {
if (this.status === 200) {
// 接受二进制文件流
var blob = this.response
that.downloadExportFile(blob, fileName)
}
}
xhr.send()
},
downloadExportFile (blob, tagFileName) {
const downloadElement = document.createElement('a')
let href = blob
if (typeof blob === 'string') {
downloadElement.target = '_blank'
} else {
href = window.URL.createObjectURL(blob) // 创建下载的链接
}
downloadElement.href = href
downloadElement.download = tagFileName
document.body.appendChild(downloadElement)
downloadElement.click() // 点击下载
document.body.removeChild(downloadElement) // 下载完成移除元素
if (typeof blob !== 'string') {
window.URL.revokeObjectURL(href) // 释放掉blob对象
}
},