久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

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

vue使用axios實(shí)現(xiàn)excel文件下載的功能

瀏覽:125日期:2022-06-12 08:33:00

前端VUE頁(yè)面上的導(dǎo)出或者下載功能,一般是調(diào)用后端的一個(gè)接口,由接口生成excel,word這些文件的流信息,返回給vue,然后由vue去構(gòu)建下載的動(dòng)作,這邊整理了一下,封裝了一下,方便以后復(fù)用。

封裝一個(gè)download文件

使用年月日時(shí)分秒毫秒做為文件的名稱,下載為excel文件

/** * 下載文件 */export const downloadFile = (url,ext, params) => { let accessToken = getStore(’accessToken’); return axios({ method: ’get’, url: `${base}${url}`, params: params, headers: { ’accessToken’: accessToken }, responseType: ’blob’, //二進(jìn)制流 }).then(res => { // 處理返回的文件流 const content = res; const blob = new Blob([content], { type: ’application/vnd.ms-excel;charset=utf-8’ }); var date = new Date().getFullYear() + '' + (new Date().getMonth() + 1) + '' + new Date().getDate() + '' + new Date().getHours() + '' + new Date().getMinutes() + '' + new Date().getSeconds() + '' + new Date().getMilliseconds(); const fileName = date + '.' + ext; 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); } });};

為具體功能封裝一個(gè)組件,方便在前臺(tái)調(diào)用

// 評(píng)價(jià)導(dǎo)出export const getRecordExport= (params) => { return downloadFile(’/record/export’,'xlsx', params)}

vue頁(yè)面上調(diào)用它,實(shí)現(xiàn)導(dǎo)出

<script>import { getReportExport} from '@/api/index';import util from '@/libs/util.js';export default { name: 'task-manage', data() {}, methods: { exportExcel() { getReportExport(this.searchForm).then(res=>{}); } }}

截圖

vue使用axios實(shí)現(xiàn)excel文件下載的功能

到此這篇關(guān)于vue使用axios實(shí)現(xiàn)excel文件下載的功能的文章就介紹到這了,更多相關(guān)vue實(shí)現(xiàn)excel文件下載內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: excel
相關(guān)文章:
主站蜘蛛池模板: 济南市| 沙河市| 余江县| 城市| 奉贤区| 大洼县| 栾城县| 阜康市| 翁源县| 福州市| 黑山县| 灌阳县| 黄浦区| 平陆县| 茌平县| 乌审旗| 孟津县| 托克逊县| 邹城市| 唐山市| 西丰县| 吉林市| 灌南县| 民县| 利津县| 肥城市| 余江县| 伊川县| 泰宁县| 宝丰县| 建宁县| 宝应县| 额尔古纳市| 荃湾区| 门源| 华阴市| 崇州市| 东台市| 新源县| 台南市| 鄱阳县|