您好,登錄后才能下訂單哦!
vue-cli+axios實現(xiàn)附件上傳下載記錄:
上傳:
這里用formData格式傳遞參數(shù);請求成功后后臺返回上傳文件的對應(yīng)信息。
重點是下載:
############## 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(); //點擊下載 document.body.removeChild(downloadElement); //下載完成移除元素 window.URL.revokeObjectURL(href); //釋放掉blob對象 } ##########################
使用blob接收文件流,中間var reg = /^["](.*)["]$/g
;為了解決下載的文件前后有_問題,把兩側(cè)的" "去掉可正常顯示;
decodeURI():對后臺返回的中文文件名url編碼進行轉(zhuǎn)碼
PS:下面看下VUE+axios上傳文件,下載文件中的一個坑。
問題描述:最近一個項目中使用axios進行上傳和下載,但是上傳和下載是需要定義responseType和headers的,這樣問題就出來了當(dāng)你沒有權(quán)限時候這個接口是拋出一個json數(shù)據(jù)的,同樣上傳格式錯誤也是一個json數(shù)據(jù)的;由于已經(jīng)定義了responseType所以接到的數(shù)據(jù)是已經(jīng)被轉(zhuǎn)換的數(shù)據(jù),它同樣會進行下載這時候就需要我們判斷返回數(shù)據(jù)時候的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 對象 document.body.removeChild(elink); } else { // IE10+下載 navigator.msSaveBlob(blob, fileName); } } } })
總結(jié)
以上所述是小編給大家介紹的vue-cli+axios實現(xiàn)文件上傳下載功能(下載接收后臺返回文件流),希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。