後臺管理系統中數據展現通常都是用表格,表格會涉及到導入和導出;vue
1.導入是利用element-ui的Upload 上傳組件;ios
<el-upload class="upload-demo" :action="importUrl"//上傳的路徑 :name ="name"//上傳的文件字段名 :headers="importHeaders"//請求頭格式 :on-preview="handlePreview"//能夠經過 file.response 拿到服務端返回數據 :on-remove="handleRemove"//文件移除 :before-upload="beforeUpload"//上傳前配置 :on-error="uploadFail"//上傳錯誤 :on-success="uploadSuccess"//上傳成功 :file-list="fileList"//上傳的文件列表 :with-credentials="withCredentials">//是否支持cookie信息發送 </el-upload>
裏面對input進行了封裝,並暴露一些方法和屬性ajax
能夠設置http-request屬性,覆蓋默認的上傳行爲,能夠自定義上傳的實現element-ui
這個通常仍是很常見的,
原理是將文件存儲到雲端,返回一個存貯地址存在本地服務器,
cos存儲過程:鑑權===分片上傳===成功返回存儲地址
tencent的cos存貯axios
導出是利用file的一個對象blob;
經過調用後臺接口拿到數據,
而後用數據來實例化blob,
利用a標籤的href屬性連接到blob對象api
//聲明導出文件名對象 const fileNames={ 1:'模板一', 2:'模板二', 3:'模板三', 4:'模板四', } export const downloadTemplate = function (scheduleType) { axios.get('/demo/template', { params: { "demoType": demoType }, responseType: 'arraybuffer' }).then((response) => { //建立一個blob對象,file的一種 let blob = new Blob([response.data], { type: 'application/x-xls' }) let link = document.createElement('a') link.href = window.URL.createObjectURL(blob) //配置下載的文件名 link.download = fileNames[scheduleType] + '_' + response.headers.datestr + '.xls' link.click() }) }
貼上整個小demo的完整代碼,在後臺開發能夠直接拿過去用(vue文件)服務器
<template> <div> <el-table ref="multipleTable" :data="tableData3" tooltip-effect="dark" border style="width: 80%" @selection-change="handleSelectionChange"> <el-table-column type="selection" width="55"> </el-table-column> <el-table-column label="日期" width="120"> <template slot-scope="scope">{{ scope.row.date }}</template> </el-table-column> <el-table-column prop="name" label="姓名" width="120"> </el-table-column> <el-table-column prop="address" label="地址" show-overflow-tooltip> </el-table-column> </el-table> <div style="margin-top: 20px"> <el-button @click="toggleSelection([tableData3[1], tableData3[2]])">切換第2、第三行的選中狀態</el-button> <el-button @click="toggleSelection()">取消選擇</el-button> <el-button type="primary" @click="importData">導入</el-button> <el-button type="primary" @click="outportData">導出</el-button> </div> <!-- 導入 --> <el-dialog title="導入" :visible.sync="dialogImportVisible" :modal-append-to-body="false" :close-on-click-modal="false" class="dialog-import"> <div :class="{'import-content': importFlag === 1, 'hide-dialog': importFlag !== 1}"> <el-upload class="upload-demo" :action="importUrl" :name ="name" :headers="importHeaders" :on-preview="handlePreview" :on-remove="handleRemove" :before-upload="beforeUpload" :on-error="uploadFail" :on-success="uploadSuccess" :file-list="fileList" :with-credentials="withCredentials"> <!-- 是否支持發送cookie信息 --> <el-button size="small" type="primary" :disabled="processing">{{uploadTip}}</el-button> <div slot="tip" class="el-upload__tip">只能上傳excel文件</div> </el-upload> <div class="download-template"> <a class="btn-download" @click="download"> <i class="icon-download"></i>下載模板</a> </div> </div> <div :class="{'import-failure': importFlag === 2, 'hide-dialog': importFlag !== 2}" > <div class="failure-tips"> <i class="el-icon-warning"></i>導入失敗</div> <div class="failure-reason"> <h4>失敗緣由</h4> <ul> <li v-for="(error,index) in errorResults" :key="index">第{{error.rowIdx + 1}}行,錯誤:{{error.column}},{{error.value}},{{error.errorInfo}}</li> </ul> </div> </div> </el-dialog> <!-- 導出 --> </div> </template> <script> import * as scheduleApi from '@/api/schedule' export default { data() { return { tableData3: [ { date: "2016-05-03", name: "王小虎", address: "上海市普陀區金沙江路 1518 弄" }, { date: "2016-05-02", name: "王小虎", address: "上海市普陀區金沙江路 1518 弄" } ], multipleSelection: [], importUrl:'www.baidu.com',//後臺接口config.admin_url+'rest/schedule/import/' importHeaders:{ enctype:'multipart/form-data', cityCode:'' }, name: 'import', fileList: [], withCredentials: true, processing: false, uploadTip:'點擊上傳', importFlag:1, dialogImportVisible:false, errorResults:[] }; }, methods: { toggleSelection(rows) { if (rows) { rows.forEach(row => { this.$refs.multipleTable.toggleRowSelection(row); }); } else { this.$refs.multipleTable.clearSelection(); } }, handleSelectionChange(val) { //複選框選擇回填函數,val返回一整行的數據 this.multipleSelection = val; }, importData() { this.importFlag = 1 this.fileList = [] this.uploadTip = '點擊上傳' this.processing = false this.dialogImportVisible = true }, outportData() { scheduleApi.downloadTemplate() }, handlePreview(file) { //能夠經過 file.response 拿到服務端返回數據 }, handleRemove(file, fileList) { //文件移除 }, beforeUpload(file){ //上傳前配置 this.importHeaders.cityCode='上海'//能夠配置請求頭 let excelfileExtend = ".xls,.xlsx"//設置文件格式 let fileExtend = file.name.substring(file.name.lastIndexOf('.')).toLowerCase(); if (excelfileExtend.indexOf(fileExtend) <= -1) { this.$message.error('文件格式錯誤') return false } this.uploadTip = '正在處理中...' this.processing = true }, //上傳錯誤 uploadFail(err, file, fileList) { this.uploadTip = '點擊上傳' this.processing = false this.$message.error(err) }, //上傳成功 uploadSuccess(response, file, fileList) { this.uploadTip = '點擊上傳' this.processing = false if (response.status === -1) { this.errorResults = response.data if (this.errorResults) { this.importFlag = 2 } else { this.dialogImportVisible = false this.$message.error(response.errorMsg) } } else { this.importFlag = 3 this.dialogImportVisible = false this.$message.info('導入成功') this.doSearch() } }, //下載模板 download() { //調用後臺模板方法,和導出相似 scheduleApi.downloadTemplate() }, } }; </script> <style scoped> .hide-dialog{ display:none; } </style> js文件,調用接口 import axios from 'axios' // 下載模板 export const downloadTemplate = function (scheduleType) { axios.get('/rest/schedule/template', { params: { "scheduleType": scheduleType }, responseType: 'arraybuffer' }).then((response) => { //建立一個blob對象,file的一種 let blob = new Blob([response.data], { type: 'application/x-xls' }) let link = document.createElement('a') link.href = window.URL.createObjectURL(blob) link.download = fileNames[scheduleType] + '_' + response.headers.datestr + '.xls' link.click() }) }
感謝看到這裏,很實用的導入導出功能代碼,歡迎交流!
聖誕節快到了,祝你們Merry Christmas!cookie