download.js
/**
* @description Download file under Web browser
* @param blob {Blob}
* @param fileName {String}
*/
export function downloadFile (blob, fileName) {
if (!blob) {
return
}
if (!(blob instanceof Blob)) {
blob = new Blob(Array(blob))
}
// IE10+
if (window.navigator.msSaveOrOpenBlob) {
navigator.msSaveBlob(blob, fileName)
} else {
let anchor = document.createElement('a')
anchor.download = fileName
anchor.style.display = 'none'
anchor.href = URL.createObjectURL(blob)
document.body.appendChild(anchor)
anchor.click()
document.body.removeChild(anchor)
URL.revokeObjectURL(anchor.href)
anchor = null
}
}
代码中引入:
import { downloadFile } from '@/utils/download-file'
async downloadImportMoudle () {
const blob = await this.$http.request({
url: xxx,
method: 'GET',
responseType: 'blob'
})
downloadFile(blob, 'name.xlsx')
}