this.$axios
.post(this.$api.tool_api + '/ip/downLoadFile', {
businessIds: businessIds.join()
})
.then(res => {
const content = res
const blob = new Blob([content])
const fileName = '測試表格123.xls'
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)
}
})
這裏用了Blob對象,上面的寫法就是用從服務器接收到的文件流(content-type:application/octet-stream)創建了一個blob對象,並使用該blob 創建一個指向類型數組的URL,將該url作爲a標籤的鏈接目標,然後去觸發a標籤的點擊事件從而實現表格下載。