使用elementui上传组件实现同一个页面上传不同类型的图片和视频
1.把upload组件抽离出去,在弹窗父组件引用子组件数据 addForm.vue代码如下:
<template>
<div class="drawer-form">
<el-drawer
title=""
:visible.sync="editrVisible"
:modal="false"
:with-header="false"
v-loading="loading"
>
<div class="upload-content">
<div class="imgBox">
<my-upload ref="upload1" :type="types[0]" :typeTitle="imgData[0].typeTitle" :resData.sync="resData1" :uploading.sync="uploading1"/>
</div>
<div class="imgBox">
<my-upload ref="upload2" :type="types[1]" :typeTitle="imgData[1].typeTitle" :resData.sync="resData2" :uploading.sync="uploading2"/>
</div>
<div class="imgBox">
<my-upload ref="upload3" :type="types[2]" :typeTitle="imgData[2].typeTitle" :resData.sync="resData3" :uploading.sync="uploading3"/>
</div>
</div>
<div class="drawer_colse" @click="cancelForm">
<i class="el-icon-close"></i>
</div>
<div class="drawer__footer">
<el-button size="mini" @click="onsubmit">确认</el-button>
<el-button size="mini" @click="cancelForm">取消</el-button>
</div>
</el-drawer>
</div>
</template>
<script>
import MyUpload from './myUpload';
export default {
components: {
MyUpload
},
data(){
return {
type:0,
types:[0,1,2,3],
uploading1:false,
uploading2:false,
uploading3:false,
resData1:[],
resData2:[],
resData3:[],
imgData: [
{typeTitle:'占道图片1'},
{typeTitle:'视频录像'},
{typeTitle:'占道图片2'},
],
imgList:[],
}
},
methods: {
cancelForm() {
this.loading = false
this.editrVisible = false;
this.$refs.upload1.clearData()
this.$refs.upload2.clearData()
this.$refs.upload3.clearData()
this.uploading1 = false
this.uploading2 = false
this.uploading3 = false
this.imgList = []
},
onsubmit(formName) { if(this.uploading1||this.uploading2||this.uploading3){
this.$message({
showClose: true,
message: '当前有文件正在上传,请稍后',
type: "warning",
});
}else{
this.loading = true
this.$refs[formName].validate((valid) => {
if (valid) {
this.addUpload(formName);
} else {
this.loading = false
return false;
}
});
}
},
addUpload(formName) {
let newArr = []
newArr = newArr.concat(this.resData1,this.resData2,this.resData3)
newArr.forEach((v,i) =>{
v.response.data.fileList.forEach((a,i) =>{
this.imgList.push(a)
})
})
let pams = {
...this.newform,
imgList:this.imgList
};
this.saveUpload(pams)
.then((res) => {
if (res.code === 1) {
this.$message({
showClose: true,
message: "上传添加成功",
type: "success",
});
this.$refs[formName].resetFields();
this.$parent.getList();
this.cancelForm()
} else{
this.$message({
showClose: true,
message: `${res.msg}`,
type: "warning",
});
this.cancelForm();
}
})
.catch((err) => {});
},
}
}
</script>
2.抽离出来的上传组件myUpload.vue如下:
<template>
<div class="imgBox-min">
<el-upload
ref="upload"
class="upload-demo"
:action="uploadAction"
:headers="uploadHeader"
:on-change="handleChange"
:data="uploadData"
:on-success="handleSuccess"
:on-remove="handleRemove"
:before-remove="beforeRemove"
:on-error="handleImageError"
:file-list="fileList"
:show-file-list="true"
:multiple="true"
:before-upload="beforeUploadPicVideo"
:auto-upload="true"
>
<slot>
<div class="titip">
<span>{{ typeTitle }}</span>
<el-button class="upBtn" size="mini" icon="el-icon-upload" plain
>上传文件</el-button
>
</div>
<div
v-if="this.fileList.length <= 0"
class="el-upload__text">暂无数据
</div>
</slot>
</el-upload>
</div>
</template>
<script>
import { getToken } from "@/utils/auth";
export default {
name: "",
props: {
uploading: {
type: Boolean,
default: false,
},
type: {
type: Number,
default: 0,
},
typeTitle: {
type: String,
default: "",
},
resData: {
type: Array,
default: [],
},
fileType: {
type: String,
default: "image",
},
},
data() {
const token = getToken();
return {
uploadAction: "https://jsonplaceholder.typicode.com/posts/",
uploadHeader: {
"Access-Token": token,
},
fileList: [],
};
},
computed: {
isUploading: {
get() {
return this.uploading;
},
set(val) {
this.$emit("update:uploading", val);
},
},
successData: {
get() {
return this.resData;
},
set(val) {
this.$emit("update:resData", val);
},
},
uploadData: {
get() {
return { type: this.type };
},
},
},
watch: {},
methods: {
clearData() {
this.$refs.upload.clearFiles();
this.successData = []
},
handleChange(file, fileList) {
let status = false
for(var i = 0; i < this.$refs.upload.uploadFiles.length;i++){
if(this.$refs.upload.uploadFiles[i].status === 'ready' || this.$refs.upload.uploadFiles[i].status === 'uploading'){
status = true;
break;
}
}
this.isUploading = status
this.fileList = fileList;
},
beforeUploadPicVideo(file) {
const typeAll = file.type.split("/");
if (this.type === 1 && typeAll[0] != "video") {
this.$message.error("此框应上传MP4视频格式");
return false;
}
if(this.type != 1 && typeAll[0] != "image"){
this.$message.error("该内容应上传图片");
return false;
}
if (typeAll[0] === "video") {
const isLt100M = file.size / 1024 / 1024 < 100;
if (["video/mp4"].indexOf(file.type) == -1) {
this.$message.error("暂时仅支持上传MP4视频格式");
return false;
}
if (!isLt100M) {
this.$message.error("上传视频大小不能超过100M哦!");
return false;
}
} else {
const isLt10M = file.size / 1024 / 1024 < 10;
if (
[
"image/jpeg",
"image/jpg",
"image/png",
"image/gif",
].indexOf(file.type) === -1
) {
this.$message.error(
"上传图片只能是png、jpg、jpeg、gif格式的图片!"
);
return false;
}
if (!isLt10M) {
this.$message.error("上传图片的大小不能超过10M");
return false;
}
}
},
handleRemove(file, fileList) {
this.handleChange(file,fileList)
this.successData = fileList;
},
handleSuccess(res, file, fileList) {
let self = this;
if (res.code === 1) {
self.successData = fileList;
this.$message({
showClose: true,
message: `${res.msg}`,
type: "success",
});
} else {
this.$message({
showClose: true,
message: `${res.msg}`,
type: "error",
});
}
},
handleImageError(err, file, fileList) {},
beforeRemove(file, fileList) {
if (this.type == 1 && ["video/mp4"].indexOf(file.raw.type) == -1) {
return true;
}
if (
this.type != 1 &&
[
"image/jpeg",
"image/jpg",
"image/png",
"image/gif",
].indexOf(file.raw.type) == -1
) {
return true;
}
return this.$confirm(`确定移除 ${file.name}?`);
},
},
};
</script>
<style lang="scss" scoped>
</style>
|