个人在使用 ElementUI Uploader 上传时 , 习惯选中/拖拽文件后预览显示本地文件 , 在提交所有信息时才进行图片上传. 官方 Demo 里关于 文件缩略图 部分 , 删除功能的实现没有写具体 , 此处进行补充

文本转自 链接

Html:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 <el-form-item label="证件照片" prop="idPhoto">
<el-upload
ref="pictureUpload"
action="https://jsonplaceholder.typicode.com/posts/"
list-type="picture-card"
:auto-upload="false"
:on-preview="handlePictureCardPreview"
:on-remove="handleRemove"
:limit="2"
>
<i slot="default" class="el-icon-plus"></i>
<div slot="file" slot-scope="{ file }">
<img class="el-upload-list__item-thumbnail" :src="file.url" alt="" />
<span class="el-upload-list__item-actions">
<span
class="el-upload-list__item-preview"
@click="handlePictureCardPreview(file)"
>
<i class="el-icon-zoom-in"></i>
</span>
<span
class="el-upload-list__item-delete"
@click="handleDownload(file)"
>
<i class="el-icon-download"></i>
</span>
<span
class="el-upload-list__item-delete"
@click="handleRemove(file)"
>
<i class="el-icon-delete"></i>
</span>
</span>
</div>
</el-upload>
<!-- 1、预览 -->
<el-dialog :visible.sync="dialogVisible">
<img width="100%" :src="dialogImageUrl" alt="预览图片" />
</el-dialog>
</el-form-item>

JS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 2、下载
handleDownload(file) {
window.open(file.url)
},
// 3、删除--根据 url 的不同进行匹配删除
handleRemove(file) {
let uploadFiles = this.$refs.pictureUpload.uploadFiles
for (var i = 0; i < uploadFiles.length; i++) {
if (uploadFiles[i]['url'] == file.url) {
uploadFiles.splice(i, 1)
}
}
},
// 点击文件列表中已上传的文件时的钩子
handlePictureCardPreview(file) {
this.dialogImageUrl = file.url
this.dialogVisible = true
},