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

<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. jsp里更新Clob类型字段数据

    ResultSet rs = null; Connection conn = new dbconn().getconnect(); Statement stmt = null; int news=0; ...

  2. 洛谷P1541 乌龟棋 [2010NOIP提高组]

    P1541 乌龟棋 题目背景 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 题目描述 乌龟棋的棋盘是一行N个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第N格是终点,游戏要求玩家 ...

  3. JQuery--关系选择器

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. LintCode刷题笔记--Flip Bits

    Flip Bits: 标签:位运算 题目:Determine the number of bits required to flip if you want to convert integer n  ...

  5. Servlet各种接口和类

    http://blog.csdn.net/jediael_lu/article/details/25036019

  6. Spring CommonsMultipartResolver上传文件小结

    自从业至今,文件上传与IO流之类的调用,一直是理解比较模糊的地方,大多就这网上搜到的资料抄抄改改草草了事,内部原理一直不甚了解,今日我们通过Spring的CommonsMultipartResolve ...

  7. 【JZOJ4772】【NOIP2016提高A组模拟9.9】运输妹子

    题目描述 小轩轩是一位非同一般的的大农(lao)场(si)主(ji),他有一大片非同一般的农田,并且坐落在一条公路旁(可以认为是数轴),在他的农田里种的东西也非同一般--不是什么水稻小麦,而是妹子. ...

  8. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十八章:立方体贴图

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十八章:立方体贴图 代码工程地址: https://github.c ...

  9. Qt qmake报错(TypeError: Property 'asciify' of object Core::Internal::UtilsJsExtension)

    问题如题. 解决方案: 第一种 用下管理员权限来打开qt creator,再创建工程.有可能是没权限创建出源码工程目录 第二种 打开qt左边的项目上,可以看到这个项目的编译路径,修改成绝对路径,或者设 ...

  10. Maximum Depth of Binary Tree 树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...