element-ui-——el-uploadexcel导入
布局文件:(选择文件放在了弹框内部——即点击导入按钮后弹框显示,先下载模板再选择文件点击提交按钮才上传)
<div class="emImport_container">
<el-dialog :title="meta.title" :visible.sync="dialogFormVisible" :modal-append-to-body="false">
<el-upload
:ref="system_id"
v-loading="uploadLoading"
class="upload-demo"
:action="action" (action: 导入文件的url地址)
:on-preview="handlePreview"
:on-remove="handleRemove"
:file-list="fileList"
:headers="headers" (headers: 请求头)
name="file" (文件名)
accept=".xlsx "
:on-error="uploadFileError"
:on-success="uploadFileSuccess"
:auto-upload="false"
:http-request="uploadFile"
:on-change="fileChange"
>
<el-button slot="trigger" size="small" type="primary">选取文件</el-button>
<el-button style="margin-left: 10px;" size="small" type="success" @click="downloadModel">下载模板</el-button>
<div slot="tip" class="el-upload__tip">请先下载模板,再选择文件上传!</div>
</el-upload>
<div slot="footer" class="dialog-footer">
<el-button class="em-button" @click="cancelUpload">取 消</el-button>
<el-button type="primary" class="em-button" @click="submitUpload">提交</el-button>
</div>
</el-dialog>
</div>
模板下载及文件导入:
<script>
import vueBus from '@/utils/vueBus'
import { emMixin } from '@/utils/mixins'
import { dataInitFn, childrenInitFn } from '@/utils/tool'
import { uploadFile } from '@/api/schoolService/parentInfo'
export default {
name: 'EmImport',
mixins: [emMixin],
data() {
return {
set: {
downloadUrl: '', // 模板下载url地址
importUrl: ''// 导入文件url地址
},
fileList: [], // 文件列表
dialogFormVisible: false,
headers: {
'Content-Type': 'multipart/form-data'
},
uploadLoading: '',
action: '',
files: [] // 选择文件
}
},
created() {
this.init()
},
methods: {
init() {
this.set = dataInitFn(this.set, this.meta)
this.children = childrenInitFn(this.children, this.componentData)
},
handleRemove(file, fileList) {
console.log(file, fileList)
},
handlePreview(file) {
console.log(file)
},
uploadFileError(err, file, fileList) {
console.log(err)
this.$message.error('文件导入失败')
},
uploadFileSuccess(response, file, fileList) {
console.log(response.data.jsonmsg.ERRORMSG)
if (response.data.jsonmsg.ERRORMSG === '') {
this.$message({
message: '恭喜你,导入成功',
type: 'success'
})
this.init()
this.dialogFormVisible = false
} else {
this.$message({
message: response.data.jsonmsg.ERRORMSG.slice(response.data.jsonmsg.ERRORMSG.indexOf('=') + 1),
type: 'error'
})
}
},
downloadModel() { // 下载导出需要的模板
window.location.href = process.env.VUE_APP_BASE_API + this.set.downloadUrl // process.env.VUE_APP_BASE_API :基础地址(env.development文件中的 base api地址
) },
// 导入csv
import() {
this.dialogFormVisible = true
this.action = process.env.VUE_APP_BASE_API + this.set.importUrl // 尤为重要,否则action是没有值的
},
fileChange(file) {
this.files.push(file.raw) // 上传文件变化时将文件对象push进files数组
},
// 上传文件
uploadFile(params) {
if (this.files) {
const formData = new FormData() // new一个formData对象
this.files = params.file (这里一定是params.file,传递给后台的应是file:(binary); 不是this.files,,否则传递给后台的是file:Undefined)
formData.append('file', this.files)
uploadFile({
url: process.env.VUE_APP_BASE_API + this.set.importUrl, //导入文件地址
params: formData (参数必须是formData)
}).then(response => {
console.log('导入结果', response)
if (response.statusCode === 200) {
this.$notify({
message: '数据导入成功',
type: 'success'
})
this.dialogFormVisible = false
vueBus.$emit('query')
} else {
this.$notify.error('数据导入失败')
}
})
}
},
submitUpload(event) {
this.$refs[this.system_id].submit() // 提交按钮
},
cancelUpload() {
this.dialogFormVisible = false
this.$message.info('已取消上传')
}
}
}
</script>
接口地址:
export function uploadFile(obj) {
return request({
url: obj.url,
method: 'post',
data: obj.params
})
}
总结:请求头格式一定是:content-Type:multipart/form-data; 否则导入会失败;传递的参数:params一定是formData(表单对象)
element-ui-——el-uploadexcel导入的更多相关文章
- 14: element ui 使用
1.1 element ui 基本使用 参考网址: http://element.eleme.io/#/zh-CN/component/button 1.初始一个vue项目并安装element ui ...
- Html | Vue | Element UI——引入使用
前言 做个项目,需要一个效果刚好Element UI有,就想配合Vue和Element UI,放在tp5.1下使用,但是引入在线的地址各种报错,本地引入就完美的解决了问题! 代码 __STATIC_J ...
- 分享一个自搭的框架,使用Spring boot+Vue+Element UI
废弃,新的:https://www.cnblogs.com/hackyo/p/10453243.html 特点:前后端分离,可遵循restful 框架:后端使用Spring boot,整合了aop.a ...
- Vue(十八)Element UI
Elment UI 1. 简介 Element UI是饿了么团队提供的一套基于Vue2.0的组件库,可以快速搭建网站,提高开发效率 ElementUI PC端 MintUI 移动端 [官网](http ...
- element ui 的Notification通知如何加 a 标签和按钮,并弹多个
前言:工作中需要在页面右下角弹出很多个提醒框,提醒框上有一个可点击的a标签,并有一个按钮,同时还需要一次性关闭所有的弹出框.转载请注明出处:https://www.cnblogs.com/yuxiao ...
- vue + element ui 实现实现动态渲染表格
前言:之前需要做一个页面,能够通过表名动态渲染出不同的表格,这里记录一下.转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9786326.html 网站地址:我的 ...
- [转]vue Element UI走马灯组件重写
https://blog.csdn.net/u013750989/article/details/82885482 1.element ui走马灯组件 -- carousel分析一波源代码:carou ...
- vue + element ui 表格自定义表头,提供线上demo
前言:工作中用到 vue+element ui 的前端框架,需要使用自定义表头,需要使用 re.转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9710826.h ...
- vue+element ui 的表格列使用组件
前言:工作中用到 vue+element ui 的前端框架,有这个场景:很多表格的列有许多一样的,所以考虑将列封装为组件.转载请注明出处:https://www.cnblogs.com/yuxiaol ...
- vue+element ui 的tab 动态增减,切换时提示用户是否切换
前言:工作中用到 vue+element ui 的前端框架,动态添加 Tab,删除 Tab,切换 Tab 时提示用户是否切换等,发现 element ui 有一个 bug,这里记录一下如何实现.转载 ...
随机推荐
- JS函数式编程 - 概念
最近在看Typescript,顺便看了一些函数式编程,然后半个国庆假期就没有了.做个笔记,分几个部分写吧. 最开始接触函数式编程的时候,第一个接触的概念就是高阶函数,和柯里化.咋一看,这不就是长期用来 ...
- SPSS操作:轻松实现1:1倾向性评分匹配(PSM)
SPSS操作:轻松实现1:1倾向性评分匹配(PSM) 谈起临床研究,如何设立一个靠谱的对照,有时候成为整个研究成败的关键.对照设立的一个非常重要的原则就是可比性,简单说就是对照组除了研究因素外,其他的 ...
- 【水滴石穿】react-native-template-app
这个也是一个基础项目 地址如下https://github.com/ndlonghi/react-native-template-app 点击登陆跳转到首页 分析代码如 react-native-te ...
- Data Lake Analytics中OSS LOCATION的使用说明
前言 Data Lake Analytic(后文简称 DLA)可以帮助用户通过标准的SQL语句直接对存储在OSS.TableStore上的数据进行查询分析. 在查询前,用户需要根据数据文件的格式和内容 ...
- [ITOO]动态建库 标签: 库数据库mysql 2016-07-17 21:23 241人阅读 评论(2) 收
最近一直在做权限系统的动态建库,动态建库,说白了就是在你点击"注册"按钮的时候,根据你输入的信息,来创建一个企业所需要的数据库的过程,因为现阶段并没有提供购买等功能,所以暂时咱们是 ...
- [转]embedding technic:从SNE到t-SNE再到LargeVis
http://bindog.github.io/blog/2016/06/04/from-sne-to-tsne-to-largevis/#top
- 2013年山东省第四届ACM大学生程序设计竞赛E题:Alice and Bob
题目描述 Alice and Bob like playing games very much.Today, they introduce a new game. There is a polynom ...
- <Mysql必知必会> ---- 笔记
转载自 https://www.jianshu.com/p/294502893128 挺基础的mysql的书籍,基本上都是如何操作的语法. 第1章 了解SQL 主键(primary key):能够唯 ...
- ES6对象的super关键字
super是es6新出的关键字,它既可以当作函数使用,也可以当作对象使用,两种使用方法不尽相同 1.super用作函数使用的时候,代表父类的构造函数,es6规定在子类中使用this之前必须先执行一次s ...
- ansible基础☞第一条命令
我的两个测试机: 系统: ubuntu 16.04.2 ansible-master: 192.168.0.107 ansible-slave: 192.168.0.108 ansible版本: ro ...