成人视屏在线观看-国产99精品-国产精品1区2区-欧美一级在线观看-国产一区二区日韩-色九九九

您的位置:首頁技術(shù)文章
文章詳情頁

vue-以文件流-blob-的形式-下載-導(dǎo)出文件操作

瀏覽:92日期:2022-12-13 13:03:50

vue項(xiàng)目中,經(jīng)常遇到文件導(dǎo)出與下載,有時(shí)候是直接返回服務(wù)端的文件url,這樣直接以a鏈接下載,或者windown.open對不同類型的文件進(jìn)行下載或預(yù)覽。但如果返回的是文件流,則需要做一些其他處理,具體形式如下:

1、首先要確定服務(wù)器返回的數(shù)據(jù)類型。

在請求頭中加入: config.responseType = ’blob’

有時(shí)候,不是所有接口都需要該類型,則可以對接口做一個(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ù)請求類型 } if (getToken()) { config.headers[’access_token’] = getToken() } return config }, error => { // Do something with request error // console.log(error) // for debug Promise.reject(error) })

2、接口請求獲取后端返回的文件流

// 導(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對象來處理數(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 對象 document.body.removeChild(elink) // 釋放標(biāo)簽}

4、在ie瀏覽器中存在兼容性問題,對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è)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Vue
相關(guān)文章:
主站蜘蛛池模板: 伊人久久国产免费观看视频 | 高清一级淫片a级中文字幕 高清一区二区 | 亚洲影院国产 | 欧美一级成人影院免费的 | 欧美自拍另类 | 国产美女一区精品福利视频 | 九九九九在线精品免费视频 | 亚洲欧洲日韩综合色天使不卡 | 一级片网址 | 国产精品久久不卡日韩美女 | 亚洲精品一区二区三区在 | 亚洲免费网站观看视频 | 国产精品久久国产精品99 | 99国产精品农村一级毛片 | 精品国产欧美一区二区最新 | 免费一级毛片视频 | 欧美特黄一级aa毛片 | 日韩欧美毛片免费观看视频 | 亚洲男人天堂网站 | 一级一片| 日本亚州视频在线八a | 国产不卡a| 亚洲国产天堂久久综合网站 | 美女视频一区二区三区在线 | 国产美女一区二区 | 国产成人精品免费视频大全办公室 | 亚洲一区三区 | 成人区精品一区二区不卡亚洲 | 国产三级在线观看播放 | 日本www高清| 国产成人精品亚洲77美色 | 夜夜操夜夜爽 | 免费人成网站 | 欧美特黄一区二区三区 | 成年网站视频在线观看 | 亚洲人成高清 | 成人午夜免费在线观看 | 欧美日本一区二区三区 | 在线视频自拍 | 亚欧在线视频 | 亚洲欧美专区精品久久 |