布局文件:(选择文件放在了弹框内部——即点击导入按钮后弹框显示,先下载模板再选择文件点击提交按钮才上传)

<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导入的更多相关文章

  1. 14: element ui 使用

    1.1 element ui 基本使用 参考网址: http://element.eleme.io/#/zh-CN/component/button 1.初始一个vue项目并安装element ui ...

  2. Html | Vue | Element UI——引入使用

    前言 做个项目,需要一个效果刚好Element UI有,就想配合Vue和Element UI,放在tp5.1下使用,但是引入在线的地址各种报错,本地引入就完美的解决了问题! 代码 __STATIC_J ...

  3. 分享一个自搭的框架,使用Spring boot+Vue+Element UI

    废弃,新的:https://www.cnblogs.com/hackyo/p/10453243.html 特点:前后端分离,可遵循restful 框架:后端使用Spring boot,整合了aop.a ...

  4. Vue(十八)Element UI

    Elment UI 1. 简介 Element UI是饿了么团队提供的一套基于Vue2.0的组件库,可以快速搭建网站,提高开发效率 ElementUI PC端 MintUI 移动端 [官网](http ...

  5. element ui 的Notification通知如何加 a 标签和按钮,并弹多个

    前言:工作中需要在页面右下角弹出很多个提醒框,提醒框上有一个可点击的a标签,并有一个按钮,同时还需要一次性关闭所有的弹出框.转载请注明出处:https://www.cnblogs.com/yuxiao ...

  6. vue + element ui 实现实现动态渲染表格

    前言:之前需要做一个页面,能够通过表名动态渲染出不同的表格,这里记录一下.转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9786326.html 网站地址:我的 ...

  7. [转]vue Element UI走马灯组件重写

    https://blog.csdn.net/u013750989/article/details/82885482 1.element ui走马灯组件 -- carousel分析一波源代码:carou ...

  8. vue + element ui 表格自定义表头,提供线上demo

    前言:工作中用到 vue+element ui 的前端框架,需要使用自定义表头,需要使用 re.转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9710826.h ...

  9. vue+element ui 的表格列使用组件

    前言:工作中用到 vue+element ui 的前端框架,有这个场景:很多表格的列有许多一样的,所以考虑将列封装为组件.转载请注明出处:https://www.cnblogs.com/yuxiaol ...

  10. vue+element ui 的tab 动态增减,切换时提示用户是否切换

    前言:工作中用到 vue+element ui 的前端框架,动态添加 Tab,删除 Tab,切换 Tab 时提示用户是否切换等,发现 element ui  有一个 bug,这里记录一下如何实现.转载 ...

随机推荐

  1. 如何用maven tycho构建自己的Eclipse RCP应用

    在你写了一个Eclipse插件之后,也许你就会想如何把它变成一个P2的项目或者是一个Java App让大家可以安装到自己的Eclipse上,dangdangdang~~ 这是你就可以利用maven-t ...

  2. LeetCode --- Validate Binary Search Tree

    题目链接 判断一颗二叉树是否是二叉搜索树(二叉排序树),也就是BST 如果该二叉树是BST, 那么对其中序遍历,所得序列一定是单调递增的(不考虑有重复数值的情况) 附上代码: /** * Defini ...

  3. JVM学习篇章(二)

     上节我们已经介绍了jvm和监控的一下方法,下面举例说明一下:  瓶颈问题定位: 内存泄漏原因定位: 1.常见的内存泄漏 2.定位的方法

  4. PPT转PDF

    需求:可以上传ppt,前台可以预览. 在用程序将ppt转pdf的过程中,遇到几个问题,记录如下: 1.检索 COM 类工厂中 CLSID 为 {91493441-5A91-11CF-8700-00AA ...

  5. SPARK-SQL内置函数之时间日期类

    转载请注明转自:http://www.cnblogs.com/feiyumo/p/8760846.html 一.获取当前时间 1.current_date获取当前日期 2018-04-09 2.cur ...

  6. WPF程序国际化

    1.创建相应的xaml文件 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presen ...

  7. TIJ——Chapter Six:Access Control

    package:the library unit The levels of access control from "most access" to "least ac ...

  8. (转)理解inode

    作者:阮一峰 原文链接:http://www.ruanyifeng.com/blog/2011/12/inode.html 一.inode是什么? 理解inode,要从文件储存说起. 文件储存在硬盘上 ...

  9. Spring Security 4 使用@PreAuthorize,@PostAuthorize, @Secured, EL实现方法安全

    [相关已翻译的本系列其他文章,点击分类里面的spring security 4] 上一篇:Spring Security 4 整合Hibernate 实现持久化登录验证(带源码) 原文地址:http: ...

  10. JavaScript 开发者都应该知道的十个概念

    1. 原始值和引用值(Value vs. Reference) 理解对象.数组和函数是如何复制和传递到函数中的.了解引用值是被复制了什么,理解原始值是通过复制值来进行复制和传递的. 简析:ECMASc ...