vue如何從后臺下載.zip壓縮包文件
vue前后端分離,使用element的el-button組件從后臺下載文件,并且解決亂碼問題
1.添加下載按鈕
![]()
2.(原始方法,會出現(xià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()這種請求方式,可以訪問后臺,但是返回的是一堆亂碼

3.(更正版)用axios({})這種方式
配置參數(shù):
- 根據(jù)返回的類型response.data.type,如果是文件流application/octet-stream則下載文件,如果是json則顯示錯誤信息;
- 文件名來自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) // 將錯誤信息顯示出來
}
reader.readAsText(response.data)
}
}).catch(error => _this.$errorMsg(error) )這樣就可以成功下載到.zip文件

4.報跨域問題

經(jīng)過排查,我這邊產(chǎn)生這個問題的原因是參數(shù)格式,如下圖,我的參數(shù)存在一個JOSN字符串,導(dǎo)致報錯

解決辦法:
使用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',??
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue keep-alive 動態(tài)刪除組件緩存的例子
今天小編就為大家分享一篇vue keep-alive 動態(tài)刪除組件緩存的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
使用Vue3進行數(shù)據(jù)綁定及顯示列表數(shù)據(jù)
這篇文章主要介紹了使用Vue3進行數(shù)據(jù)綁定及顯示列表數(shù)據(jù),整篇文章圍繞Vue3進行數(shù)據(jù)綁定及顯示列表數(shù)據(jù)的想換自來哦展開內(nèi)容,需要的小伙伴可以參考一下2021-10-10
vue elementui el-table 表格里邊展示四分位圖效果(功能實現(xiàn))
這篇文章主要介紹了vue elementui el-table 表格里邊展示四分位圖效果(功能實現(xiàn)),本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-04-04
vue2.* element tabs tab-pane 動態(tài)加載組件操作
這篇文章主要介紹了vue2.* element tabs tab-pane 動態(tài)加載組件操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07

