el-upload 自定义上传图片踩的坑(不使用action)
项目需求
- 添加时上传图片限制9张,每张不超过2M
- 编辑时需要回显已经上传成功的图片
- 支持删除和再次上传图片
<el-upload
action="#"
list-type="picture-card"
:on-remove="removeZL"
:file-list="zlFileList"
:before-upload="beforeUpload"
:on-exceed="maxError"
:http-request="httpRequest"
:limit="9"
>
<i class="el-icon-plus"></i>
</el-upload>
this.item = this.$route.query.item;
this.item.zlList.map((ele) => {
this.zlFileList.push({ id: ele.imageId, url: ele.uri, name: ele.fileName });
});
maxError(file, fileList) {
if (fileList.length === 9) {
this.$message.error("最多可上传9张图片!");
}
},
httpRequest(pic) {
const fd = new FormData();
fd.append("file", pic.file);
api.upload(fd).then((res) => {
this.zlFileList.push({
id: res.body.imageId,
url: res.body.uri,
name: res.body.fileName,
});
});
},
>>> .el-upload-list__item.is-ready,
>>> .el-upload-list__item.is-uploading {
display: none !important;
}