vue如何從后臺(tái)下載.zip壓縮包文件
vue前后端分離,使用element的el-button組件從后臺(tái)下載文件,并且解決亂碼問題
1.添加下載按鈕
2.(原始方法,會(huì)出現(xiàn)亂碼)給按鈕添加點(diǎn)擊事件
添加接口代碼
download: function() { const row = this.tableRadio // console.log(row) axios.get('/api/download', { params: { reportRuleId: row.reportRuleId } }).then(response => { console.log(response.data); }).catch(error => { this.$message.error(error) }) }
一直使用的是axios.get()這種請(qǐng)求方式,可以訪問后臺(tái),但是返回的是一堆亂碼
3.(更正版)用axios({})這種方式
配置參數(shù):
- 根據(jù)返回的類型response.data.type,如果是文件流application/octet-stream則下載文件,如果是json則顯示錯(cuò)誤信息;
- 文件名來自http頭部的Content-Disposition字段;
axios({ method: 'GET', url: '/api/download', params: { reportRuleId: row.reportRuleId }, responseType: 'blob' }).then(response => { if (response.data.type === 'application/octet-stream') { // 獲取http頭部的文件名信息,若無需重命名文件,將下面這行刪去 const fileName = response.headers['content-disposition'].split('=')[1] /* 兼容ie內(nèi)核,360瀏覽器的兼容模式 */ if (window.navigator && window.navigator.msSaveOrOpenBlob) { const blob = new Blob([response.data], { type: 'application/zip' }) window.navigator.msSaveOrOpenBlob(blob, fileName) } else { /* 火狐谷歌的文件下載方式 */ const blob = new Blob([response.data], { type: 'application/zip' }) const url = window.URL.createObjectURL(blob) const link = document.createElement('a') // 創(chuàng)建a標(biāo)簽 link.href = url link.download = fileName // 文件重命名,若無需重命名,將該行刪去 link.click() URL.revokeObjectURL(url) // 釋放內(nèi)存 } resolve(response) } else { const reader = new FileReader() reader.onload = function(event) { const msg = JSON.parse(reader.result).data _this.$errorMsg(message) // 將錯(cuò)誤信息顯示出來 } reader.readAsText(response.data) } }).catch(error => _this.$errorMsg(error) )
這樣就可以成功下載到.zip文件
4.報(bào)跨域問題
經(jīng)過排查,我這邊產(chǎn)生這個(gè)問題的原因是參數(shù)格式,如下圖,我的參數(shù)存在一個(gè)JOSN字符串,導(dǎo)致報(bào)錯(cuò)
解決辦法:
使用qs模塊將params參數(shù)格式轉(zhuǎn)換一下 ,將下面這行代碼放在params后面
paramsSerializer: params => { return qs.stringify(params, { arrayFormat: 'brackets' }) }
type:'application/zip'可以更改下載文件的類型
具體有以下這些類型:
'doc'??????? => 'application/msword', ??? 'bin'??????? => 'application/octet-stream', ??? 'exe'??????? => 'application/octet-stream', ??? 'so'??????? => 'application/octet-stream', ??? 'dll'??????? => 'application/octet-stream', ??? 'pdf'??????? => 'application/pdf', ??? 'ai'??????? => 'application/postscript', ??? 'xls'??????? => 'application/vnd.ms-excel', ??? 'ppt'??????? => 'application/vnd.ms-powerpoint', ??? 'dir'??????? => 'application/x-director', ??? 'js'??????? => 'application/x-javascript', ??? 'swf'??????? => 'application/x-shockwave-flash', ??? 'xhtml'??????? => 'application/xhtml+xml', ??? 'xht'??????? => 'application/xhtml+xml', ??? 'zip'??????? => 'application/zip', ??? 'mid'??????? => 'audio/midi', ??? 'midi'??????? => 'audio/midi', ??? 'mp3'??????? => 'audio/mpeg', ??? 'rm'??????? => 'audio/x-pn-realaudio', ??? 'rpm'??????? => 'audio/x-pn-realaudio-plugin', ??? 'wav'??????? => 'audio/x-wav', ??? 'bmp'??????? => 'image/bmp', ??? 'gif'??????? => 'image/gif', ??? 'jpeg'??????? => 'image/jpeg', ??? 'jpg'??????? => 'image/jpeg', ??? 'png'??????? => 'image/png', ??? 'css'??????? => 'text/css', ??? 'html'??????? => 'text/html', ??? 'htm'??????? => 'text/html', ??? 'txt'??????? => 'text/plain', ??? 'xsl'??????? => 'text/xml', ??? 'xml'??????? => 'text/xml', ??? 'mpeg'??????? => 'video/mpeg', ??? 'mpg'??????? => 'video/mpeg', ??? 'avi'??????? => 'video/x-msvideo', ??? 'movie'??????? => 'video/x-sgi-movie',??
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue實(shí)現(xiàn)網(wǎng)絡(luò)圖片瀑布流 + 下拉刷新 + 上拉加載更多(步驟詳解)
這篇文章主要介紹了vue實(shí)現(xiàn)網(wǎng)絡(luò)圖片瀑布流 + 下拉刷新 + 上拉加載更多,本文分步驟通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01vue項(xiàng)目element-ui級(jí)聯(lián)選擇器el-cascader回顯的問題及解決
這篇文章主要介紹了vue項(xiàng)目element-ui級(jí)聯(lián)選擇器el-cascader回顯的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07Vue生產(chǎn)和開發(fā)環(huán)境如何切換及過濾器的使用
本文主要介紹了Vue生產(chǎn)、開發(fā)環(huán)境如何切換及過濾器的使用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08Vue循環(huán)組件加validate多表單驗(yàn)證的實(shí)例
今天小編就為大家分享一篇Vue循環(huán)組件加validate多表單驗(yàn)證的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09將VUE項(xiàng)目部署到服務(wù)器的詳細(xì)步驟
經(jīng)過一段時(shí)間的探索,前端后端都有大致的樣子了,下面就是部署到服務(wù)器,這篇文章主要給大家介紹了關(guān)于將VUE項(xiàng)目部署到服務(wù)器的詳細(xì)步驟,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08解決vue使用vant下拉框van-dropdown-item 綁定title值不變問題
這篇文章主要介紹了解決vue使用vant下拉框van-dropdown-item 綁定title值不變問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08Laravel 如何在blade文件中使用Vue組件的示例代碼
這篇文章主要介紹了Laravel 如何在blade文件中使用Vue組件,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06Vue父組件調(diào)用子組件函數(shù)實(shí)現(xiàn)
這篇文章主要介紹了Vue父組件調(diào)用子組件函數(shù)實(shí)現(xiàn),全文通過舉例子及代碼的形式進(jìn)行了一個(gè)簡(jiǎn)單的介紹,希望大家能夠理解并且學(xué)習(xí)到其中知識(shí)2021-08-08Vue CLI3移動(dòng)端適配(px2rem或postcss-plugin-px2rem)
這篇文章主要介紹了Vue CLI3移動(dòng)端適配(px2rem或postcss-plugin-px2rem),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04