<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&nbsp;&nbsp;(请上传小于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:' 图片上传路径' ,
  
ruleForm:{
  

  logoUrl:'',
},
progressNum:false,
}
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上传文件的更多相关文章

  1. Element-ui上传文件(删除、添加、预览)

    先看下效果是不是你所需要的.... 上传文件进度条后续会加上的.... 功能需求:默认为上传状态 1.未上传:点击可上传文件 2.已上传:点击可上传文件 (1).鼠标移入[删除] (2).鼠标点击[预 ...

  2. elementUI 上传文件图片大小加了限制后 仍然上传了

    https://blog.csdn.net/chanlingmai5374/article/details/80558444  看了这位老哥的说法 在看看文档 才发现自己没认真看文档 <el-u ...

  3. element-ui上传文件带token

    template> <el-upload action="test" :headers="myHeaders"></el-upload& ...

  4. element-ui上传组件,通过自定义请求上传文件

    记录使用element-ui上传组件,通过自定义请求上传文件需要注意的地方. <el-upload ref="uploadMutiple" :auto-upload=&quo ...

  5. element-ui上传多个文件时会发送多个请求

    1. element-ui的默认 默认是异步多次请求上传单个文件 如果业务就是单纯的上传文件,那么这个样子是没有问题的 前端代码参考 https://element-plus.gitee.io/#/z ...

  6. vue + elementUi + upLoadIamge组件 上传文件到阿里云oss

    <template> <div class="upLoadIamge"> <el-upload action="https://jsonpl ...

  7. Vue上传文件:ElementUI中的upload实现

    一.上传文件实现 两种实现方式: 1.直接action <el-upload  .利用before-upload属性 此种方式有个弊端,就是action是必选的参数,那么action如果和pos ...

  8. elementUI 上传.csv文件不成功 导入功能

    前言:element上传excel文件   导入功能 目标:点击导入,将excel表格的数据填充到表格. <el-upload class="upload-demo" :ac ...

  9. element-ui upload上传文件并携带参数 使用formData对象

    需求:上传文件的时候,需要携带其他的参数 问题:使用upload上传文件时,必须使用formData对象,而其他的参数通过data获取的到的,formData和data是不能同时传输的 解决:获取到的 ...

随机推荐

  1. xpython在Windos下的安装及简单的文本打开、保存

    前几天写自动化部署脚本,用的是paramiko和shell相结合,paramiko可是实现ssh登录,文件及文件夹的上传下载,这些功能,然后一直想自己写个东西出来,于是就想把这些功能我把他放到图形化界 ...

  2. .gitlab-ci.yml 配置文件,知识点

    官方介绍:https://docs.gitlab.com/ee/ci/yaml/README.html 翻译: https://segmentfault.com/a/1190000010442764

  3. [visual studio]visual studio 2017激活码

    企业版:NJVYC-BMHX2-G77MM-4XJMR-6Q8QF 专业版:KBJFW-NXHK6-W4WJM-CRMQB-G3CDH

  4. Matlab关于视觉问题中的一些自有API

    [randsample/randperm] y = randsample(n,k);从1:n中随机抽取k个数. y=  randperm(n)或者y=  randperm(n,k) [rectint] ...

  5. AES对称加密和解密(转)

    AES对称加密和解密 package demo.security; import java.io.IOException; import java.io.UnsupportedEncodingExce ...

  6. 记录一个Q版openstack搭建教程地址

    https://blog.csdn.net/networken/article/details/80682437 感谢这篇文章的作者,文档很详细,记录一下,希望对大家有帮助.

  7. centos 7.5安装docker-CE 18

    1.查看系统版本 cat /etc/centos-release CentOS Linux release 7.5.1804 (Core) uname -r 3.10.0-862.el7.x86_64 ...

  8. [UE4]RichTextBlock

    RichTextBlock:富文本 一.新建一个名为“TestRichTextBlock_0”的UserWidget,并添加名为RichTextBlock_0的RichTextBlock控件,并设置T ...

  9. JS HTML倒计时 进入页面

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

  10. 同步对象(同步条件Event)

    event = threading.Event()   #创建同步对象 event.wait()     #同步对象等待状态 event.set() #同步对象设置Trueevent.clear()  ...