1、后端接口文档:
导入:POST /import [http:-192.168.1.1-%.....xlsx]
2、前端API获取:
//导入
export function eventImport(obj) {
return request({
url: '/api/accident/import',
method: 'post',
data: obj
})
}
3、前端页面:
<el-button type="primary" icon="" @click="importFile()" v-if="curFileList.length">确认导入</el-button>
<el-upload style="display: inline-block;margin-left: 10px" v-if="!curFileList.length" class="upload-demo" ref="upload" action="#" accept=".xls,.xlsx" :on-remove="handleRemove" :file-list="curFileList" :auto-upload="false" :show-file-list="false"
:on-change="handleChange">
<el-button type="primary" icon="">导入模板</el-button>
</el-upload>
<a href="http:/192.168.1.1.....xlsx" style="color: #fff;margin-left: 10px;font-size: 12px;">下载模板</a>
import { eventAdd, eventDel, eventImport, eventPage, eventUpdate } from '@/api/screen/eventData'; //获取列表数据api
export default {
components: {},
data() {
return {
curFileList: [],
};
},
methods: {
/* 导入数据功能 */
importFile() {
const self = this;
if (this.curFileList.length == 0) {
this.$notify({
title: '提示',
message: '请上传Excel文件',
type: 'warning',
duration: 2000,
});
return;
}
let formData = new FormData();
this.curFileList.forEach((val, index) => {
formData.append('file', val.raw);
})
eventImport(formData).then((res) => {
self.$notify({
title: '成功',
message: '导入成功',
type: 'success',
duration: 2000,
});
self.curFileList = [];
self.getDataList();
});
},
handleChange(file, fileList) {
this.curFileList = [];
this.curFileList.push(file);
},
handleRemove(file) {
this.curFileList = [];
},
},
};