vue下載excel文件的四種方法實(shí)例
1、通過url下載
即后端提供文件的地址,直接使用瀏覽器去下載
通過window.location.href = 文件路徑下載
window.location.href = `${location.origin}/operation/ruleImport/template`
通過 window.open(url, '_blank')
window.open(`${location.origin}/operation/ruleImport/template`)
這兩種使用方法的不同:
window.location:當(dāng)前頁跳轉(zhuǎn),也就是重新定位當(dāng)前頁;只能在網(wǎng)站中打開本網(wǎng)站的網(wǎng)頁。
window.open:在新窗口中打開鏈接;可以在網(wǎng)站上打開另外一個(gè)網(wǎng)站的地址。
2、通過 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í)使用新的文件名稱。
前端創(chuàng)建超鏈接,接收后端的文件流:
axios.get(`/operation/ruleImport/template`, { responseType: "blob" //服務(wù)器響應(yīng)的數(shù)據(jù)類型,可以是 '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ì)象來處理數(shù)據(jù),并設(shè)置文件類型 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ò)展名,如果沒有設(shè)置瀏覽器將自動(dòng)檢測(cè)正確的文件擴(kuò)展名并添加到文件。
3、通過 js-file-download 插件
安裝:
npm install js-file-download --S
使用
import fileDownload from 'js-file-download' axios.get(`/operation/ruleImport/template`, { responseType: 'blob' //返回的數(shù)據(jù)類型 }) .then(res => { fileDownload(res.data, this.fileName) })
4、使用fetch下載
exportFile() { fetch('http://127.0.0.1:8765/course/exportCourse/33', { method: 'GET', headers: new Headers({ 'Authorization': Cookie.get('Authorization') }), }) .then(res => res.blob()) .then(data => { const blobUrl = window.URL.createObjectURL(data); const a = document.createElement('a'); a.download = this.fileName+'.xlsx'; a.href = blobUrl; a.click(); }); },
總結(jié)
到此這篇關(guān)于vue下載excel文件的四種方法的文章就介紹到這了,更多相關(guān)vue下載excel文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue中實(shí)現(xiàn)上傳文件給后臺(tái)實(shí)例詳解
在本文里小編給大家分享了一篇關(guān)于vue中實(shí)現(xiàn)上傳文件給后臺(tái)的實(shí)例內(nèi)容,有需要此功能的可以學(xué)習(xí)參考下。2019-08-08Vue2.0父組件與子組件之間的事件發(fā)射與接收實(shí)例代碼
這篇文章主要介紹了Vue2.0父組件與子組件之間的事件發(fā)射與接收實(shí)例代碼,需要的朋友可以參考下2017-09-09vue簡單實(shí)現(xiàn)轉(zhuǎn)盤抽獎(jiǎng)
這篇文章主要為大家詳細(xì)介紹了vue簡單實(shí)現(xiàn)轉(zhuǎn)盤抽獎(jiǎng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03vue實(shí)現(xiàn)下拉框的多選功能(附后端處理參數(shù))
本文介紹了如何使用Vue實(shí)現(xiàn)下拉框的多選功能,實(shí)現(xiàn)了在選擇框中選擇多個(gè)選項(xiàng)的功能,文章詳細(xì)介紹了實(shí)現(xiàn)步驟和示例代碼,對(duì)于想要了解如何使用Vue實(shí)現(xiàn)下拉框多選功能的讀者具有一定的參考價(jià)值2023-08-08vue3中g(shù)etCurrentInstance示例講解
這篇文章主要給大家介紹了關(guān)于vue3中g(shù)etCurrentInstance的相關(guān)資料,文中還介紹了Vue3中關(guān)于getCurrentInstance的大坑,需要的朋友可以參考下2023-03-03