标题
<el-upload
class="editavatar-uploader"
action="/api/imageInfo/uploadImage/"
:data="this.imgJobNum"
:on-success="handleAvatarSuccess"
:before-upload="beforeAvatarUpload"
:file-list="fileList"
:show-file-list="false"
>
<img v-if="imageSrc" :src="imageSrc" class="avatar" />
<i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
数据
data(){
return{
fileList: [],
imgJobNum: { imageJobNum: "" },
}
}
上传前和上传成功的方法
created() {
this.imgJobNum.imageJobNum = JSON.parse(localStorage.getItem("baseJobNum"));
},
methods: {
handleAvatarSuccess(res, file) {
this.editForm.baseImage = URL.createObjectURL(file.raw);
this.imageSrc = this.editForm.baseImage;
},
beforeAvatarUpload(file) {
const isJPG = file.type === ("image/jpeg" || "image/png" || "image/jpg");
if (!isJPG) {
this.$message.error("上传头像图片只能是 JPG、JPEG 或 png 格式!");
}
return isJPG;
},
}