<el-upload
class="avatar-uploader"
:action="importFileUrl"
:with-credentials="true"
:show-file-list="false"
:on-error="uploadError"
:on-success="handleAvatarSuccess"
:before-upload="beforeAvatarUpload"
accept="image/png, image/jpg, image/jpeg, image/gif, image/PNG, image/JGP, image/JEPG, image/GIF"
>
<img v-if="imageUrl" :src="imageUrl" class="avatar" />
<i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
data里面初始化数据:
importFileUrl: "/visualize/user/getBase64",
imageUrl: "",
responseData: "",
js:
handleAvatarSuccess (response, file) {
if (response.success === true) {
this.responseData = response.data //传给后端,z这个值是base64编码的
//注释的这种方法也能显示照片
/* let imgUrl = "data:image/png;base64,"
this.imageUrl = imgUrl+response.data; */
//这种方法也能显示照片
this.imageUrl = URL.createObjectURL(file.raw);
this.$message.success("上传头像成功!")
}
},
uploadError (response, file) {
this.$message.success("上传失败,请重试!")
},
beforeAvatarUpload (file) {
var isJPG = /^image\/(jpeg|png|jpg|gif|JPEG|PNG|JPG|GIF)$/.test(file.type)
const isLt2M = file.size / 1024 / 1024 < 2;
if (!isJPG) {
this.$message.error("图片上传格式不正确!")
return
}
if (!isLt2M) {
this.$message.error("图片大小不能超过 2MB!")
}
return isJPG && isLt2M
},