vue-cli+axios實(shí)現(xiàn)文件上傳下載功能(下載接收后臺(tái)返回文件流)
vue-cli+axios實(shí)現(xiàn)附件上傳下載記錄:
上傳:
這里用formData格式傳遞參數(shù);請(qǐng)求成功后后臺(tái)返回上傳文件的對(duì)應(yīng)信息。
重點(diǎn)是下載:
##############
downloadfile(res) {
var blob = new Blob([res.data], {type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8'}); //application/vnd.openxmlformats-officedocument.wordprocessingml.document這里表示doc類型
var contentDisposition = res.headers['content-disposition']; //從response的headers中獲取filename, 后端response.setHeader("Content-disposition", "attachment; filename=xxxx.docx") 設(shè)置的文件名;
var patt = new RegExp("filename=([^;]+\\.[^\\.;]+);*");
var result = patt.exec(contentDisposition);
var filename = result[1];
var downloadElement = document.createElement('a');
var href = window.URL.createObjectURL(blob); //創(chuàng)建下載的鏈接
var reg = /^["](.*)["]$/g;
downloadElement.style.display = 'none';
downloadElement.href = href;
downloadElement.download = decodeURI(filename.replace(reg,"$1")); //下載后文件名
document.body.appendChild(downloadElement);
downloadElement.click(); //點(diǎn)擊下載
document.body.removeChild(downloadElement); //下載完成移除元素
window.URL.revokeObjectURL(href); //釋放掉blob對(duì)象
}
##########################
使用blob接收文件流,中間var reg = /^["](.*)["]$/g;為了解決下載的文件前后有_問(wèn)題,把兩側(cè)的" "去掉可正常顯示;
decodeURI():對(duì)后臺(tái)返回的中文文件名url編碼進(jìn)行轉(zhuǎn)碼
PS:下面看下VUE+axios上傳文件,下載文件中的一個(gè)坑。
問(wèn)題描述:最近一個(gè)項(xiàng)目中使用axios進(jìn)行上傳和下載,但是上傳和下載是需要定義responseType和headers的,這樣問(wèn)題就出來(lái)了當(dāng)你沒(méi)有權(quán)限時(shí)候這個(gè)接口是拋出一個(gè)json數(shù)據(jù)的,同樣上傳格式錯(cuò)誤也是一個(gè)json數(shù)據(jù)的;由于已經(jīng)定義了responseType所以接到的數(shù)據(jù)是已經(jīng)被轉(zhuǎn)換的數(shù)據(jù),它同樣會(huì)進(jìn)行下載這時(shí)候就需要我們判斷返回?cái)?shù)據(jù)時(shí)候的headers是否為文件以外的定義,然后將blob數(shù)據(jù)轉(zhuǎn)化為json數(shù)據(jù)即可。代碼如下
下載文件為例:
let requsetFile = (params,baseurl,url) =>{
axios({
method: 'get',
baseURL:baseurl,
url: url,
headers: {'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}, //定義相應(yīng)頭
responseType: 'blob', //定義
data:params.query || {}
})
.then(function(res){
params.success && params.success(res)
})
.catch(function(error){
params.error && params.error(error)
})
}
//下面為判斷headers和轉(zhuǎn)化blob為json
api.Templet({
success:res=>{
const isEXCLE = res.headers["content-type"] === ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" || "application/vnd.ms-excel");
if(!isEXCLE){
let reader = new FileReader();
reader.onload = e => this.$message.error(JSON.parse(e.target.result).msg);
reader.readAsText(res.data);
}else {
let blob = new Blob([res.data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
const fileName = `模板文件${Date.parse(new Date())}.xlsx`;
if ('download' in document.createElement('a')) { // 非IE下載
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); // 釋放URL 對(duì)象
document.body.removeChild(elink);
} else { // IE10+下載
navigator.msSaveBlob(blob, fileName);
}
}
}
})
總結(jié)
以上所述是小編給大家介紹的vue-cli+axios實(shí)現(xiàn)文件上傳下載功能(下載接收后臺(tái)返回文件流),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
相關(guān)文章
Vuejs 2.0 子組件訪問(wèn)/調(diào)用父組件的方法(示例代碼)
這篇文章主要介紹了Vuejs 2.0 子組件訪問(wèn)/調(diào)用父組件的方法(示例代碼),需要的朋友可以參考下2018-02-02
Vue?vant-ui框架實(shí)現(xiàn)上拉加載下拉刷新功能
功能需求——獲取后端接口返回的數(shù)據(jù),實(shí)現(xiàn)列表數(shù)據(jù)上滑加載更多下一頁(yè)數(shù)據(jù),下拉數(shù)據(jù)刷新功能,結(jié)合vant-ui框架實(shí)現(xiàn)??芍苯訁⒖际褂?/div> 2022-09-09
Vue動(dòng)態(tài)構(gòu)建混合數(shù)據(jù)Treeselect選擇樹(shù)及巨樹(shù)問(wèn)題的解決
這篇文章主要介紹了Vue動(dòng)態(tài)構(gòu)建混合數(shù)據(jù)Treeselect選擇樹(shù)及巨樹(shù)問(wèn)題的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
Vue狀態(tài)管理庫(kù)Pinia詳細(xì)介紹
這篇文章主要介紹了Vue3-pinia狀態(tài)管理,pinia是 vue3 新的狀態(tài)管理工具,簡(jiǎn)單來(lái)說(shuō)相當(dāng)于之前 vuex,它去掉了 Mutations 但是也是支持 vue2 的,需要的朋友可以參考下2022-08-08
關(guān)于element-ui?單選框默認(rèn)值不選中的解決
這篇文章主要介紹了關(guān)于element-ui?單選框默認(rèn)值不選中的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09最新評(píng)論

