vue-以文件流-blob-的形式-下載-導(dǎo)出文件操作
vue項(xiàng)目中,經(jīng)常遇到文件導(dǎo)出與下載,有時(shí)候是直接返回服務(wù)端的文件url,這樣直接以a鏈接下載,或者windown.open對(duì)不同類型的文件進(jìn)行下載或預(yù)覽。但如果返回的是文件流,則需要做一些其他處理,具體形式如下:
1、首先要確定服務(wù)器返回的數(shù)據(jù)類型。
在請(qǐng)求頭中加入: config.responseType = 'blob'
有時(shí)候,不是所有接口都需要該類型,則可以對(duì)接口做一個(gè)判定:
// request攔截器 service.interceptors.request.use( config => { // 根據(jù)接口判定 if ( config.url === '/setting/exportData' || config.url.indexOf('export') > -1 || config.url.indexOf('Export') > -1) { config.responseType = 'blob' // 服務(wù)請(qǐng)求類型 } if (getToken()) { config.headers['access_token'] = getToken() } return config }, error => { // Do something with request error // console.log(error) // for debug Promise.reject(error) } )
2、接口請(qǐng)求獲取后端返回的文件流
// 導(dǎo)出 onExport() { if (this.dataList === '') { this.$message({ type: 'error', message: '暫無數(shù)據(jù)導(dǎo)出' }) return } const fd = new FormData() fd.append('id', this.id) var exportFileName = '導(dǎo)出文件名' //設(shè)置導(dǎo)出的文件名,可以拼接一個(gè)隨機(jī)值 exportData(fd) .then(res => { // res.data 是后端返回的文件流 // 調(diào)用 downloadUrl 處理文件 downloadUrl(res.data, exportFileName) }) .catch(err => { this.$message({ type: 'error', message: err.message }) }) },
3、文件處理downloadUrl--該方法可以寫為公共方法以便調(diào)用
// 使用iframe框架下載文件--以excel為例,可修改type與fileName選擇文件類型 export function downloadUrl(res, name) { const blob = new Blob([res], { type: 'application/vnd.ms-excel' }) // 構(gòu)造一個(gè)blob對(duì)象來處理數(shù)據(jù) const fileName = name + '.xlsx' // 導(dǎo)出文件名 const elink = document.createElement('a') // 創(chuàng)建a標(biāo)簽 elink.download = fileName // a標(biāo)簽添加屬性 elink.style.display = 'none' elink.href = URL.createObjectURL(blob) document.body.appendChild(elink) elink.click() // 執(zhí)行下載 URL.revokeObjectURL(elink.href) // 釋放URL 對(duì)象 document.body.removeChild(elink) // 釋放標(biāo)簽 }
4、在ie瀏覽器中存在兼容性問題,對(duì)downloadUrl做一些調(diào)整
// 使用iframe框架下載文件 -兼容性考慮 export function downloadUrl(res, name) { const blob = new Blob([res], { type: 'application/vnd.ms-excel' }) // for IE if (window.navigator && window.navigator.msSaveOrOpenBlob) { const fileName = name + '.xlsx' window.navigator.msSaveOrOpenBlob(blob, fileName) } else { // for Non-IE (chrome, firefox etc.) const fileName = name + '.xlsx' 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) document.body.removeChild(elink) } }
總結(jié):至此,以文件流的形式導(dǎo)出文件的一種方式便已經(jīng)實(shí)現(xiàn)。
補(bǔ)充知識(shí):vue中使用文件流進(jìn)行下載(new Blob),不打開一個(gè)新頁面下載
我就廢話不多說了,大家還是直接看代碼吧~
export function download (url, params, filename) { Message.warning('導(dǎo)出數(shù)據(jù)中') return axios.get(url, { params: params, responseType:'arraybuffer', }).then((r) => { const content = r.data const blob = new Blob([content],{type:'application/vnd.ms-excel'}) if ('download' in document.createElement('a')) { 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) document.body.removeChild(elink) Message.success('導(dǎo)出成功') } }).catch((r) => { console.error(r) Message.error('導(dǎo)出失敗') }) }
以上這篇vue-以文件流-blob-的形式-下載-導(dǎo)出文件操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue3中動(dòng)態(tài)修改樣式與級(jí)聯(lián)樣式優(yōu)先順序圖文詳解
在項(xiàng)目中,我們時(shí)常會(huì)遇到動(dòng)態(tài)的去綁定操作切換不同的CSS樣式,下面這篇文章主要給大家介紹了關(guān)于Vue3中動(dòng)態(tài)修改樣式與級(jí)聯(lián)樣式優(yōu)先順序的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04在vue3項(xiàng)目中實(shí)現(xiàn)國際化的代碼示例
國際化就是指在一個(gè)項(xiàng)目中,項(xiàng)目中的語言可以進(jìn)行切換(中英文切換),那么在實(shí)際項(xiàng)目中是如何實(shí)現(xiàn)的呢,本文就給大家詳細(xì)的介紹實(shí)現(xiàn)方法,需要的朋友可以參考下2023-07-07Vue Router4中params傳參失效和報(bào)錯(cuò)問題的解決方法
在使用vue-router4中params 進(jìn)行路由組件之間傳參,跳轉(zhuǎn)頁面接收不了并報(bào)錯(cuò),本文給大家介紹了Vue Router4中params傳參失效和報(bào)錯(cuò)問題的解決方法,需要的朋友可以參考下2024-03-03vscode 使用Prettier插件格式化配置使用代碼詳解
這篇文章主要介紹了vscode 使用Prettier插件格式化配置使用,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08詳解vuex數(shù)據(jù)傳輸?shù)膬煞N方式及this.$store undefined的解決辦法
這篇文章主要介紹了vuex數(shù)據(jù)傳輸?shù)膬煞N方式 及 this.$store undefined的解決辦法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08vue-vuex中使用commit提交mutation來修改state的方法詳解
今天小編就為大家分享一篇vue-vuex中使用commit提交mutation來修改state的方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09