<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. django基础 -- 10.form , ModelForm ,modelformset

    一.生成页面可用的 HTML标签 1.form 所有内置字段 Field required=True, 是否允许为空 widget=None, HTML插件 label=None, 用于生成Label ...

  2. spring中@Value("${key}")值原样输出${key}分析与解决

    问题: 最近发现一个项目中,在类中通过@Value("${key}")获取配置文件中变量值突然不行了,直接输出${key},示例代码如下: java类中: import org.s ...

  3. delphi字符串分割

    function GetLeft(sText, sepStr: string): string; var p: Integer; begin p := Pos(sepStr, sText); then ...

  4. CenterOS7.5中搭建wordpress

    centeros7.5中搭建wordpress 1.环境 云平台:华为云 服务器操作系统:CentOS7.: 博客部署的服务器:Apache HTTP: 数据库:mysql: 框架:wordpress ...

  5. [蓝桥杯]ALGO-188.算法训练_P0504

    Anagrams指的是具有如下特性的两个单词:在这两个单词当中,每一个英文字母(不区分大小写)所出现的次数都是相同的.例如,Unclear和Nuclear.Rimon和MinOR都是Anagrams. ...

  6. git比较两个分支的文件的差异

    Git diff branch1 branch2 --stat   //显示出所有有差异的文件列表 Git diff branch1 branch2 文件名(带路径)   //显示指定文件的详细差异 ...

  7. 个人测试SQL学习

    --普通方法赋值 declare @a int print @a --Update 方法赋值 ) ' print @name --Select 语句赋值 ) ' print @name ) selec ...

  8. k8s学习笔记之一:kubernetes简介

    一.虚拟化技术 1.什么是虚拟化技术 虚拟化,是指通过虚拟化技术将一台计算机虚拟为多台逻辑计算机.在一台计算机上同时运行多个逻辑计算机,每个逻辑计算机可运行不同的操作系统,并且应用程序都可以在相互独立 ...

  9. Web框架本质及第一个Django实例 Web框架

    Web框架本质及第一个Django实例   Web框架本质 我们可以这样理解:所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端. 这样我们就可以自己实现Web ...

  10. java中异常处理

    看到一篇异常处理的好文章: Java异常处理机制主要依赖于try,catch,finally,throw,throws五个关键字. try 关键字后紧跟一个花括号括起来的代码块,简称try块.同理:下 ...