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是不能同时传输的 解决:获取到的 ...
随机推荐
- 1.1.15 word调整文字与下划线之间的间距
先请按CTRL+U快捷键,或点击“下划线”按钮,然后输入一个空格,再输入文字“下划线间距”,在文字的尾部再添加一个空格.选中文字内容(注意不要选中首尾的空格),单击菜单“格式”→“字体”,在“字体”设 ...
- ant design + react,自动获取上传音频的时长(react-audio-player)
在后台管理项目中,用户要求上传音频,并且自动获取音频时长. 第一步, import { Upload, Button, Icon } from 'antd'; 第二步,在表单中使用 Upload 组件 ...
- Struts1 不能进入Action,页面空白问题解决
http://blog.csdn.net/barry_liao/article/details/35592935 Struts1 不能进入Action,页面空白问题解决 标签: Struts1Acti ...
- Windows10右键添加"在此处打开命令窗口"
添加注册表: Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Directory\shell\OpenCmdHere] @=" ...
- git push 和 pull 时 免密执行的方法
问题:在使用git代码仓库时,使用git clone 获取代码时,如果使用的是https协议,则在每次push时需要输入账号密码.相关文档:文档一,文档二 验证了文档一种的方法二可用,记录一下创建文件 ...
- ado.net调用带参数的sql语句
- Java八大排序算法
Java八大排序算法: package sort; import java.util.ArrayList; import java.util.Arrays; import java.util.List ...
- Java锁----Lock实现原理
转载. https://blog.csdn.net/wl6965307/article/details/51249156 Lock完全用Java写成,在java这个层面是无关JVM实现的. 在java ...
- 解决strcmp的错误以及VS的快捷键
主要是C++数组作业中发现的一些问题. 第一点是关于strcat函数 我用VS2018调用strcat的时候报错,错误信息提示strcat不安全(?)要用strcat_s.修改后,可成功运行. 但这两 ...
- logback的使用和logback.xml详解,在Spring项目中使用log打印日志
logback的使用和logback.xml详解 一.logback的介绍 Logback是由log4j创始人设计的另一个开源日志组件,官方网站: http://logback.qos.ch.它当前分 ...