要求導(dǎo)出excel文件。當(dāng)點(diǎn)擊下載模板或下載反饋結(jié)果,axios發(fā)起后端接口請(qǐng)求,返回的數(shù)據(jù)獲取 response 時(shí)出現(xiàn)亂碼,或者火狐瀏覽器下面下載的時(shí)候,文件不識(shí)別,或者加了其他的文件名,
這里面采取了兩條思路:
1.其他瀏覽器直接點(diǎn)a標(biāo)簽打開(kāi)
2.火狐瀏覽器采取文件流的下載方式,axios封裝
1、通過(guò) url 下載
即后端提供文件的地址,直接使用瀏覽器去下載
通過(guò) window.location.href = 文件路徑 下載
window.location.href = `${location.origin}/operation/ruleImport/template`
通過(guò) window.open(url, '_blank')
window.open(`${location.origin}/operation/ruleImport/template`)
這兩種使用方法的不同:
window.location:當(dāng)前頁(yè)跳轉(zhuǎn),也就是重新定位當(dāng)前頁(yè);只能在網(wǎng)站中打開(kāi)本網(wǎng)站的網(wǎng)頁(yè)。
window.open:在新窗口中打開(kāi)鏈接;可以在網(wǎng)站上打開(kāi)另外一個(gè)網(wǎng)站的地址。
2、通過(guò) a 標(biāo)簽的 download 屬性結(jié)合 blob 構(gòu)造函數(shù)下載
a 標(biāo)簽的 download 屬性是 HTML5 標(biāo)準(zhǔn)新增的,作用是觸發(fā)瀏覽器的下載操作而不是導(dǎo)航到下載的url,這個(gè)屬性可以設(shè)置下載時(shí)使用新的文件名稱(chēng)。
前端創(chuàng)建超鏈接,接收后端的文件流:
axios.get(`/operation/ruleImport/template`, {
responseType: "blob" //服務(wù)器響應(yīng)的數(shù)據(jù)類(lèi)型,可以是 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream',默認(rèn)是'json'
})
.then(res =>
if(!res) return
const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' }) // 構(gòu)造一個(gè)blob對(duì)象來(lái)處理數(shù)據(jù),并設(shè)置文件類(lèi)型
if (window.navigator.msSaveOrOpenBlob) { //兼容IE10
navigator.msSaveBlob(blob, this.filename)
} else {
const href = URL.createObjectURL(blob) //創(chuàng)建新的URL表示指定的blob對(duì)象
const a = document.createElement('a') //創(chuàng)建a標(biāo)簽
a.style.display = 'none'
a.href = href // 指定下載鏈接
a.download = this.filename //指定下載文件名
a.click() //觸發(fā)下載
URL.revokeObjectURL(a.href) //釋放URL對(duì)象
}
// 這里也可以不創(chuàng)建a鏈接,直接window.open(href)也能下載
})
.catch(err => {
console.log(err)
})
注:請(qǐng)求后臺(tái)接口時(shí)要在請(qǐng)求頭上加{responseType: 'blob'};download 設(shè)置文件名時(shí),可以直接設(shè)置擴(kuò)展名,如果沒(méi)有設(shè)置瀏覽器將自動(dòng)檢測(cè)正確的文件擴(kuò)展名并添加到文件。
——————————————————————————————————————————
自己在vue項(xiàng)目中用到的方法,可做參考
// 下載
handleDowload (id) {
var userAgent = navigator.userAgent
if (userAgent.indexOf('Firefox') > -1) {
console.log(userAgent)
axios({ // 用axios發(fā)送post請(qǐng)求
method: 'get',
url: process.env.VUE_APP_BASE_API + '/print/export_excel' + `?pe_id=${id}&&download=all`, // 請(qǐng)求地址
responseType: 'blob', // 表明返回服務(wù)器返回的數(shù)據(jù)類(lèi)型
headers: {
'Content-Type': 'application/json'
}
}).then((res) => {
const stream = res.data // 后端用stream返回Excel文件
const blob = new Blob([stream])
// 前端獲取業(yè)務(wù)碼,成功執(zhí)行正常業(yè)務(wù)
const downloadElement = document.createElement('a')
const href = window.URL.createObjectURL(blob) // 創(chuàng)建下載的鏈接
downloadElement.href = href
downloadElement.download = '付印單.xlsx'// 下載后文件名
document.body.appendChild(downloadElement)
downloadElement.click() // 點(diǎn)擊下載
document.body.removeChild(downloadElement) // 下載完成移除元素
window.URL.revokeObjectURL(href) // 釋放掉blob對(duì)象
})
} else {
this.urlForDownloadall = process.env.VUE_APP_BASE_API + '/print/export_excel' + `?pe_id=${id}&&download=all`
window.location.href = this.urlForDownloadall
}
}