<template>
<div class="upload-container">
<el-upload
:data="dataObj"
multiple
:limit="limit"
:list-type="listType"
:show-file-list="true"
class="image-uploader"
drag
:on-exceed="outLimit"
:file-list="fileList"
:auto-upload="true"
:http-request="uploadSectionFile"
action="123"
>
<i class="el-icon-upload" />
<div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
</el-upload>
</div>
</template>
<script>
import { uploadpic } from "@/api/xy_fileupload";
import { getToken } from "@/utils/auth";
export default {
name: "SingleImageUpload",
props: {
limit: { type: Number, default: 1 }, //限定数量
listType: { type: String, default: "text" } //限定展示方式 text/picture/picture-card
},
data() {
return {
fileList: [],
tempUrl: "",
dataObj: { Authorization: "" }
};
},
mounted() {
this.init();
},
methods: {
uploadSectionFile(param) {
var fileObj = param.file;
// FormData 对象
var form = new FormData();
// 文件对象
form.append("file", fileObj);
let _this = this;
uploadpic(form).then(res => {
if (res.code === 10000) {
_this.value = res.picUrl;
// 上传成功
_this.$emit("uploadSuccess", res.picUrl);
} else {
// 上传失败
_this.$emit("error", res.message);
}
});
},
init() {
this.dataObj.Authorization = getToken();
},
//文件数量超出限制
outLimit() {
// this.$emit("outLimit","文件超出限制" );
this.$message({
message: "文件数量超出限制",
type: "warning"
});
},
rmImage() {
this.emitInput("");
}
}
};
</script>
<style lang="scss" scoped>
@import "~@/styles/mixin.scss";
.upload-container {
width: 100%;
position: relative;
@include clearfix;
.image-uploader {
width: 100%;
float: left;
}
.image-preview {
width: 10%;
height: 10%;
position: relative;
border: 1px dashed #d9d9d9;
float: left;
margin-left: 50px;
.image-preview-wrapper {
position: relative;
width: 100%;
height: 100%;
img {
width: 100%;
height: 100%;
}
}
.image-preview-action {
position: absolute;
width: 100%;
height: 100%;
left: 0;
top: 0;
cursor: default;
text-align: center;
color: #fff;
opacity: 0;
font-size: 20px;
background-color: rgba(0, 0, 0, 0.5);
transition: opacity 0.3s;
cursor: pointer;
text-align: center;
line-height: 200px;
.el-icon-delete {
font-size: 36px;
}
}
&:hover {
.image-preview-action {
opacity: 1;
}
}
}
}
</style>
VUE elementUI upload图片上传组件demo
最新推荐文章于 2024-06-22 10:41:50 发布