element-ui upload组件上传


方法一:
<el-table-column label="操作">
<template slot-scope="scope">
<el-button icon="el-icon-circle-plus-outline" type="primary" v-on:click="addOp(scope.row)"></el-button>
<el-button type="primary" v-on:click="importQuato(scope.row)">导入额度批次表</el-button>
</template>
</el-table-column>
<el-button type="primary" v-on:click="importQuato(scope.row)">导入额度批次表</el-button>//导入按钮
scope.row可以获取每一列的id <el-dialog :title="title" :visible.sync="dialogVisible">
<el-upload
class="upload-demo"
drag
class='ensure ensureButt'
action="123" //这里可以随意不影响
:before-upload="beforeAvatarUpload" //上传前文件校验
multiple>
<i class="el-icon-upload"></i>
<div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
<div class="el-upload__tip" slot="tip">只能上传xls、xlsx文件,且不超过10MB</div>
</el-upload>
<div slot="footer" class="dialog-footer">
<!--<el-button v-on:click="dialogVisible = false">取 消</el-button>-->
<el-button type="primary" v-on:click="dialogVisible = false">确 定</el-button>
</div>
</el-dialog>
// 上传前对文件的大小的判断
beforeAvatarUpload (file) {
var fileName=new Array()
fileName =file.name.split('.');
const extension = fileName[fileName.length-1] === 'xls'
const extension2 = fileName[fileName.length-1]=== 'xlsx'
const isLt2M = file.size / 1024 / 1024 < 10
if (!extension && !extension2) {
this.$message({
message: '上传模板只能是xls、xlsx格式!',
type: 'warning'
});
// console.log('上传模板只能是xls、xlsx格式!')
}
if (!isLt2M) {
this.$message({
message: '上传模板大小不能超过 10MB!',
type: 'warning'
});
// console.log('上传模板大小不能超过 10MB!')
}
if (extension || extension2 && isLt2M == true) {
console.log(file)
let fd = new FormData()
fd.append('invoiceTypeId', this.invoice_type_id)//随文件上传的其他参数
fd.append('epid', this.epid)
fd.append('file', file)
// console.log(fd)
this.newImport(fd).then(function (res) {//校验完成后提交
console.log(res)
}, function () {
console.log('failed');
});
return true
}
return extension || extension2 && isLt2M
},
//提示信息
open: function (msg, code) {
if (code == '000') {
this.$alert(msg, '提示', {
confirmButtonText: '确定',
type: 'success',
callback: action => {
this.dialogFormVisible = false;
location.reload();
}
});
} else {
this.$alert(msg, '提示', {
confirmButtonText: '确定',
type: 'error',
callback: action => {
this.dialogFormVisible = false;
location.reload();
}
});
}
},
newImport (data) {
this.$http.post('../enterPriseQuota/importEnterPriseQuota', data).then(function (res) {//成功后回调
let code = res.data.returncode;//返回json结果
let msg = res.data.msg;
this.open(msg, code);
console.log('success');
}, function () {
console.log('failed');
});
},
}
@RequestMapping("/importEnterPriseQuota")
@ResponseBody
public Map importEnterPriseQuota(@RequestParam(value = "invoiceTypeId") String invoiceTypeId,
@RequestParam(value = "epid") String epid,
@RequestParam("file") MultipartFile proFile, HttpServletRequest request) {
String fileDir = request.getSession().getServletContext().getRealPath("/tmp");
File dir = new File(fileDir);
Map resMap = null;
File file = null;
try {
file = new File(fileDir, proFile.getOriginalFilename());
if (!dir.exists()) {
dir.mkdir();
}
if (!file.exists()) {
file.createNewFile();
}
proFile.transferTo(file);
Date a = new Date();
resMap = enterPriseQuotaService.importEnterPriseQuato(invoiceTypeId,file,epid);
Date b = new Date();
log.info("************all_time*************************" + (b.getTime() - a.getTime()));
return resMap;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (file != null && file.exists()) {
file.delete();
}
}
resMap.put("returncode", "999");
resMap.put("msg", "程序异常,请联系管理员");
return resMap;
}
方法二:
<el-dialog :title="tagName" :visible.sync="dialogVisible">
<el-upload
class="upload-demo"
drag
class='ensure ensureButt'
:action="importFileUrl"//在初始时指定url地址即可
:on-error="uploadError"
:on-success="uploadSuccess"
:before-upload="beforeAvatarUpload"
multiple>
<i class="el-icon-upload"></i>
<div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
<div class="el-upload__tip" slot="tip">只能上传xls、xlsx文件,且不超过10MB</div>
</el-upload>
<div slot="footer" class="dialog-footer">
<!--<el-button v-on:click="dialogVisible = false">取 消</el-button>-->
<el-button type="primary" v-on:click="dialogVisible = false">确 定</el-button>
</div>
</el-dialog>
//有时候 :on-success,:on-error 这个函数会无法调用,之前看另一个帖子是用的:onError="uploadError" :onSuccess="uploadSuccess"
http://blog.csdn.net/qq_39685062/article/details/77036582
2018-01-12: 昨天又遇到上传成功但是无法调用成功回调函数的问题,这里涉及到vue的生命周期,导致无法调用,js也不会报错,把对应函数放到methods顶部可解决。
// 上传成功后的回调
uploadSuccess (response) {
let code = response.returncode;
let msg = response.msg;
this.open(msg, code);
},
// 上传错误
uploadError (response) {
this.open("500", "文件导入异常!");
},
@RequestMapping("inEmployee")
@ResponseBody
public Map inEmployee(HttpServletRequest servletRequest) {
MultipartHttpServletRequest request = (MultipartHttpServletRequest) servletRequest;
Iterator<String> itr = request.getFileNames();
MultipartFile proFile = null;
while (itr.hasNext()) {
String str = itr.next();
proFile = request.getFile(str);
}
String fileDir = request.getSession().getServletContext().getRealPath("/tmp");
File dir = new File(fileDir);
Map resMap = null;
File file = null;
try {
file = new File(fileDir, proFile.getOriginalFilename());
if (!dir.exists()) {
dir.mkdir();
}
if (!file.exists()) {
file.createNewFile();
}
proFile.transferTo(file);
Date a = new Date();
resMap = employeeService.insEm(file,fileDir);
Date b = new Date();
log.info("************all_time*************************" + (b.getTime() - a.getTime()));
return resMap;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (file != null && file.exists()) {
file.delete();
}
}
resMap.put("returncode", "999");
resMap.put("msg", "程序异常,请联系管理员");
return resMap;
}
element-ui upload组件上传的更多相关文章
- React antd如何实现<Upload>组件上传附件再次上传已清除附件缓存问题。
最近在公司做React+antd的项目,遇到一个上传组件的问题,即上传附件成功后,文件展示处仍然还有之前上传附件的缓存信息,需要解决的问题是,要把上一次上传的附件缓存在上传成功或者取消后,可以进行清除 ...
- vue watch 监听element upload组件上传成功返回的url列表
因为 on-success 上传成功返回的是一个异步的结果....如果父组件需要这个结果的话 必须用watch 监听 然后里面建立一个 save()方法 save方法里面再调用接口 传需要的上传之后的 ...
- element ui实现手动上传文件,且只能上传单个文件,并能覆盖上传。
element ui提供了成熟的组件场景,但实际工作中难免会遇到认(sha)真(diao)的产品.比如,最近遇到的,要求实现手动上传特定格式文件(用户点击“上传文件”按钮,确定之后,只是单纯选择了文件 ...
- Vue+Element UI 实现视频上传
一.前言 项目中需要提供一个视频介绍,使用户能够快速.方便的了解如何使用产品以及注意事项. 前台使用Vue+Element UI中的el-upload组件实现视频上传及进度条展示,后台提供视频上传AP ...
- Ant Design Upload 组件上传文件到云服务器 - 七牛云、腾讯云和阿里云的分别实现
在前端项目中经常遇到上传文件的需求,ant design 作为 react 的前端框架,提供的 upload 组件为上传文件提供了很大的方便,官方提供的各种形式的上传基本上可以覆盖大多数的场景,但是对 ...
- Element UI中的上传文件功能
上传文件给后台: <el-upload style="display:inline-block" :limit=" class="upload-demo& ...
- VUE -- iview table 组件 中使用 upload组件 上传组件 on render 事件不会触发问题
碰到的问题是: upload 组件在 on中写的监听事件不会被触发 在 props 中来监听:==>
- 关于本地使用antd的upload组件上传文件,ngnix报错405的问题
使用阿里的ui框架antd的upload,会自动请求ngnix上面的一个路径,也就是action所在的位置,一直报错405 not allowed,后来经讨论,统一将action写成一个路径,后端对这 ...
- 使用element的upload组件实现一个完整的文件上传功能(上)
说到标题就有点心塞了,前段时间项目上需要实现一个文件上传的功能,然后就咔咔的去用了element的upload组件,不用不知道一用吓一跳哇. 在使用的过程中遇到了很多让意想不到的问题,后来也因为时间问 ...
随机推荐
- Android 常用正则表达式
前言 闲扯一下,已经有好久没更新博客了,记得上一篇博客的更新时间为 2017-05-12 15:20.截止到今天,超过一百天没更新了. 这篇博客的内容大多数是从别的博客摘抄过来的,写这篇博客的目的主要 ...
- 公历和农历转换的JS代码(车)
<!-- function CalConv(M) { FIRSTYEAR = 1936; LASTYEAR = 2031; LunarCal = [ new tagLunarCal(23, 3, ...
- HTML阻止冒泡事件的发生
阻止事件冒泡函数(低级标签的点击事件触发后,上级标签的点击事件再触发,此函数就是防止冒泡事件发生) function stopEventBubble(event){ var e=event || wi ...
- Confluence 6 增加和减少你许可证的用户数
增加你许可证的用户数 如果你使用的用户数超过你许可证允许的用户数的话,你的 Confluence 实例将会变成为只读. 只读的意思是没有用户可以创建和编辑内容,直到你的用户数满足你的许可证需求为止. ...
- 放一点百度来的,常见的windowserror
0操作成功完成.1功能错误.2系统找不到指定的文件.3系统找不到指定的路径.4系统无法打开文件.5拒绝访问.6句柄无效.7存储控制块被损坏.8存储空间不足,无法处理此命令.9存储控制块地址无效.10环 ...
- vuejs项目---配置理解:
当我们需要和后台分离部署的时候,必须配置config/index.js: 用vue-cli 自动构建的目录里面 (环境变量及其基本变量的配置) 1 2 3 4 5 6 7 8 9 10 11 12 ...
- 用D3.js画的人物关系demo
代码下载地址:https://github.com/zhangzn3/group-explorer ### Demo1功能 *** * 支持节点拖拽 * 支持节点拖拽并固定位置 * 支持鼠标浮到节点显 ...
- 【python】打印函数调用栈
traceback.print_stack()
- poj2728 生成树01分数规划 (二分答案)
给定整数序列a,b,求出下式的最大值 sum{ai*xi}/sum{bi*xi},xi=0|1 通俗来说,就是选出一些整数对(ai,bi),使得选出的a之和与选出的b之和商最大化 二分答案L,即选出的 ...
- python 利用split读取文本文件中每一行的数字并保存至相应文件夹
import re from numpy import * def getStr(file_path,file_path1): fp = open(file_path, 'r') op = open( ...
