1.用axios做接口請求。
2.URL.createObjectURL()
靜態方法會創建一個 DOMString
,其中包含一個表示參數中給出的對象的URL。這個 URL 的生命周期和創建它的窗口中的 document
綁定。這個新的URL 對象表示指定的 File
對象或 Blob
對象。
function downFile(url, params) {
axios({
method: 'get',
url,
params,
responseType: 'arraybuffer'
}).then(res => {
let blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
let objectUrl = URL.createObjectURL(blob)
//創建a標簽鏈接并點擊 start
let link = document.createElement('a')
link.style.display = 'none';
link.href = objectUrl
if (params.filename) {
link.download = `${params.filename}.xlsx`
}
document.body.appendChild(link);
link.click();
//創建a標簽鏈接并點擊 end
document.body.removeChild(link)
window.URL.revokeObjectURL(blob)
})
}