elementui上传文件
<el-form-item label="上传附件">
<transition name="fade">
<el-upload ref="upload" class="upload-demo" drag :action="fileUploadPath"
accept=".doc,.docx,.xls,.xlsx,.ppt,.pptx,.pdf,.zip,.rar"
:on-success="handleUploadSuccess" :on-remove="handleUploadRemove"
:before-upload="handleBeforeUpload" :file-list="this.ruleForm.fileJson">
<i class="el-icon-upload"></i>
<div class="el-upload__text">将文件拖到此处(火狐不支持),或<em>点击上传</em></div>
<div class="el-upload__tip" slot="tip">支持格式:doc,docx,xls,xlsx,ppt,pptx,pdf,zip,rar (请上传小于8M的文件)。</div>
</el-upload>
</transition>
</el-form-item>
主要依靠的是handleUploadSuccess上传成功执行的方法
fileUploadPath这个字段是你上传的文件路径
data 里面定两个变量fileIdsList,sourceFileList 数据类型为函数
handleUploadRemove(file, fileList) {//删除类似
this.fileIdsList = [];
this.sourceFileList = [];
fileList.map(res => {
if (res.response) {
this.fileIdsList.push(res.response.data.id);
this.sourceFileList.push({
name: res.response.data.fileName,
url: res.response.data.accessUrl,
fileUrl: res.response.data.fileUrl,
id: res.response.data.id
});
} else {
this.fileIdsList.push(res.id);
this.sourceFileList.push({
name: res.fileName,
url: res.accessUrl,
fileUrl: res.fileUrl,
id: res.id
});
}
})
},
handleBeforeUpload(file){//文件过滤,html用了accept,不过不考虑兼容下面可以不要
var FileExt = file.name.replace(/.+\./, ""); if (['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf', 'zip', 'rar']
.indexOf(FileExt.toLowerCase()) === -)
{ this.$message({ type: 'warning', message: '请上传后缀名为[doc,docx,xls,xlsx,ppt,pptx,pdf,zip,rar]的附件!' }); return false; } },
handleUploadSuccess(response,files,fileList){//这里是自带的3个参数
if(response.code!==){
this.$message({
type: 'warning',
message: response.msg
});
//this.$refs.upload.clearFiles();
this.fileIdsList = [];
this.sourceFileList = [];
for (let i = ; i < fileList.length - ; i++) {//数据会有两种可能
if (fileList[i].response) {
this.fileIdsList.push(fileList[i].response.data.id);
this.sourceFileList.push({
name: fileList[i].response.data.originalFileName,
url: fileList[i].response.data.accessUrl,
fileUrl: fileList[i].response.data.fileUrl,
id: fileList[i].response.data.id
});
} else {
this.fileIdsList.push(fileList[i].id);
this.sourceFileList.push({
name: fileList[i].name,
url: fileList[i].url,
fileUrl: fileList[i].fileUrl,
id: fileList[i].id
});
}
}//这里主要是处理假设上传失败,获取失败之前成功的图片
}else{
this.$message({
message: "上传成功!",
type: 'success'
});
this.fileIdsList=[];
this.sourceFileList = [];
fileList.map(res=>{
if(res.response){
this.fileIdsList.push(res.response.data.id);
this.sourceFileList.push({
name: res.response.data.fileName,
url: res.response.data.accessUrl,
fileUrl: res.response.data.fileUrl,
id: res.response.data.id
});
}else{
this.fileIdsList.push(res.id);
this.sourceFileList.push({
name: res.fileName,
url: res.accessUrl,
fileUrl: res.fileUrl,
id: res.id
});
}
})
}
let mesfileLists=[];//把数据处理成后端给的接口需要的样子
this.sourceFileList.forEach((item)=>{
let mesfile={};//定一个对象存字段
mesfile.fileName=item.name;
mesfile.url=item.fileUrl;
mesfile.fileId= item.id;
mesfileLists.push(mesfile);//然后push到一个函数里面
})
if(this.sourceFileList){
this.ruleForm.fileJson=mesfileLists;
}
编辑页面同理
注意,你如果数据格式像我这样的在编辑页面你还要处理下
fileList.map(res=>{
if(res.response){
this.fileIdsList.push(res.response.data.id);
this.sourceFileList.push({
name: res.response.data.fileName,
url: res.response.data.accessUrl,
fileUrl: res.response.data.fileUrl,
id: res.response.data.id
});
}else{
this.fileIdsList.push(res.id);
this.sourceFileList.push({
name: res.name,
url: res.accessUrl,
fileUrl: res.fileUrl,
id: res.id
});
}
})
他们两个读的名字不一样,我被坑了下,所以留下博客留念
补充elementui图片上传
单图上传
<p class="logoLoading" v-if="progressNum">图片过大,上传中,请稍后...</p>
<div class="logoBox" v-if="ruleForm.logoUrl">
<img :src="ruleForm.logoUrl" class="logo">
<div class="img-del-btn" @click="clearLogo">
<div class="del-box">
<i class="fa fa-trash-o" aria-hidden="true"></i>
</div>
</div>
</div>
<el-upload
class="logo-uploader"
:action="UploadPath"
:show-file-list="false"
:before-upload="handleBeforeLogo"
:on-progress="handleLogoing"
:on-success="handleLogo"
accept=".image,.jpg,.jpeg,.image,.bmp,.image,.gif,.png">
<el-button class="logo_update">上传图标</el-button>
</el-upload> data(){
UploadPath:' 图片上传路径' ,
}
methods:{
handleBeforeLogo(file){
if (['image/jpeg', 'image/bmp', 'image/gif', 'image/png'].indexOf(file.type) === -) {
this.$message.error('上传图片仅支持.jpg .jpeg .gif .png .bmp图片格式!');
return false;
}
return true;
},
handleLogoing(event,file,fileList){
if (file.size / / >= ) {
this.progressNum=true
}
},
handleLogo(res){
if(res.data.url){
this.ruleForm.logoUrl =res.data.url;
this.progressNum=false;
}
},
clearLogo(){
this.ruleForm.logoUrl=''
}
}
多图上传
<ul class="bannerUl">
<li class="bannerLi" :style="`backgroundImage:url(${bannerItem.url})`" v-for="(bannerItem,index) in bannerList" :key="index">
<div class="img-del-btn" @click.stop="handleBannerRemove(index)">
<div class="del-box">
<i class="fa fa-trash-o" aria-hidden="true"></i>
</div>
</div>
</li>
</ul>
<el-upload
class="bannerAdd"
:action="UploadPath"
:before-upload="handleBeforeBanner"
:on-success="handleBannerSuccess" accept=".image,.jpg,.jpeg,.image,.bmp,.image,.gif,.png">
<i class="el-icon-plus bannerAddIcon"></i>
<span class="bannerAddTitle">添加轮播图<br/><span style="font-size:12px;">建议1200*360</span></span>
</el-upload>
methods:{
handleBeforeBanner(file){
if (['image/jpeg', 'image/bmp', 'image/gif', 'image/png'].indexOf(file.type) === -1) {
this.$message.error('上传图片仅支持.jpg .jpeg .gif .png .bmp图片格式!');
return false;
}
if (file.size / 1024 / 1024 >= 2) {
this.$message.error('上传图片大小不能超过 2MB!');
return false;
}
return true;
},
handleBannerSuccess(res, file, fileList){
let flag=true;
if(this.bannerList&&this.bannerList.length>4){
this.$message({
type: 'error',
message: '轮播图最多上传5张!'
});
flag=false;
}else{
this.bannerList.push(res.data);
flag=true;
} },
}
elementui上传文件的更多相关文章
- Element-ui上传文件(删除、添加、预览)
先看下效果是不是你所需要的.... 上传文件进度条后续会加上的.... 功能需求:默认为上传状态 1.未上传:点击可上传文件 2.已上传:点击可上传文件 (1).鼠标移入[删除] (2).鼠标点击[预 ...
- elementUI 上传文件图片大小加了限制后 仍然上传了
https://blog.csdn.net/chanlingmai5374/article/details/80558444 看了这位老哥的说法 在看看文档 才发现自己没认真看文档 <el-u ...
- element-ui上传文件带token
template> <el-upload action="test" :headers="myHeaders"></el-upload& ...
- element-ui上传组件,通过自定义请求上传文件
记录使用element-ui上传组件,通过自定义请求上传文件需要注意的地方. <el-upload ref="uploadMutiple" :auto-upload=&quo ...
- element-ui上传多个文件时会发送多个请求
1. element-ui的默认 默认是异步多次请求上传单个文件 如果业务就是单纯的上传文件,那么这个样子是没有问题的 前端代码参考 https://element-plus.gitee.io/#/z ...
- vue + elementUi + upLoadIamge组件 上传文件到阿里云oss
<template> <div class="upLoadIamge"> <el-upload action="https://jsonpl ...
- Vue上传文件:ElementUI中的upload实现
一.上传文件实现 两种实现方式: 1.直接action <el-upload .利用before-upload属性 此种方式有个弊端,就是action是必选的参数,那么action如果和pos ...
- elementUI 上传.csv文件不成功 导入功能
前言:element上传excel文件 导入功能 目标:点击导入,将excel表格的数据填充到表格. <el-upload class="upload-demo" :ac ...
- element-ui upload上传文件并携带参数 使用formData对象
需求:上传文件的时候,需要携带其他的参数 问题:使用upload上传文件时,必须使用formData对象,而其他的参数通过data获取的到的,formData和data是不能同时传输的 解决:获取到的 ...
随机推荐
- Linux内核原理第八次作业
Linux内核如何装载和启动一个可执行程序 一.ELF可执行文件格式 ELF格式分类: 可重定位文件:用来和其他object文件一起创建可执行文件和共享文件 可执行文件:指出应该从哪里开始执行 共享文 ...
- Nginx反向代理tomcat返回400 bad request
Nginx反向代理tomcat返回400 bad request nginx 版本1.12, tomcat版本 9.06 最近用Nginx做反向代理tomcat,实现前后端分离,nginx 将请求代理 ...
- 2018年,Java程序员转型大数据开发,是不是一个好选择?
近日网上有一篇关于Java程序员职场生存现状的文章“2017年 Java 程序员,风光背后的危机”,在Java程序员圈子里引起了广泛关注和热议. 2017年,Java 程序员面临更加激烈的竞争. 不得 ...
- Windows下好用的git客户端--GitExtentions
用git: https://git-scm.com/downloads GitExtentions: https://sourceforge.net/projects/gitextensions/ B ...
- lambda函数的特性
lambda表达式可以理解为一种抽象的函数实现方法,这种方式只有最基本的三个步骤:给与参数,表达式实现,返回结果.这种方式非常干净,减少了内存的使用,整个程序少了函数的污染,代码格式也会更为简练.但在 ...
- Python输入语句
什么是输入 咱们在银行ATM机器前取钱时,肯定需要输入密码,对不? 那么怎样才能让程序知道咱们刚刚输入的是什么呢?? 大家应该知道了,如果要完成ATM机取钱这件事情,需要先从键盘中输入一个数据,然后用 ...
- problem:vue组件局部刷新,在组件销毁(destroyed)时取消刷新无效问题
场景: 一个群发消息列表(数组) 列表下有多条消息(元素) 每条正在发送的消息数据状态需要实时刷新,发送完成时需要显示成功提示符合且不需要刷新,然后3秒消失.首次显示列表时,已经成功的状态不显示这个成 ...
- CentOS7和CentOS6怎样开启MySQL远程访问
CentOS6开启MySQL远程访问 1.开放MySQL访问端口3306 修改防火墙配置文件 vi /etc/sysconfig/iptables 加入端口配置 -A INPUT -m s ...
- node和npm的安装和镜像源的修改
在node官网下载https://nodejs.org/en/ 直接下载msi的文件,需要配置环境变量 我的电脑-->属性-->高级系统配置-->环境变量-->用户变量,在用户 ...
- scrapy-pipeline的方法
scrapy中多个pipeline作用: 一个项目可能需要爬取多个网站,根据每个网站的数据量(处理方式)不同,可创建多个管道 pipeline class SpideranythingPipeline ...