uploadImageUrl.vue
2.89 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<template>
<div class="upload-image-container">
<div class="image-preview" v-for="(image, index) in images" :key="index">
<el-image
style="width: 100px; height: 100px; margin-right: 10px;"
:src="image.url"
fit="cover"
:preview-src-list="previewList">
</el-image>
<i class="el-icon-delete delete-icon" @click="removeImage(index)"></i>
</div>
<el-upload
v-if="images.length < imageCount"
class="upload-button"
action=""
:show-file-list="false"
:before-upload="beforeUpload"
:http-request="handleUpload">
<i class="el-icon-plus"></i>
</el-upload>
</div>
</template>
<script>
import { uploadFile } from '@/api/common'
export default {
name: 'UploadImageUrl',
props: {
imageCount: {
type: Number,
default: 1
}
},
data() {
return {
images: [],
previewList: []
}
},
methods: {
beforeUpload(file) {
const isImage = file.type.indexOf('image/') === 0
const isLt2M = file.size / 1024 / 1024 < 2
if (!isImage) {
this.$message.error(this.$t('upload.imageTypeError'))
}
if (!isLt2M) {
this.$message.error(this.$t('upload.imageSizeError'))
}
return isImage && isLt2M
},
async handleUpload({ file }) {
try {
const formData = new FormData()
formData.append('uploadFile', file)
const response = await uploadFile(formData)
this.images.push(response.data)
this.previewList = this.images.map(img => img.url)
this.$emit('change', this.images)
} catch (error) {
this.$message.error(error.message || this.$t('upload.uploadFailed'))
}
},
removeImage(index) {
this.images.splice(index, 1)
this.previewList = this.images.map(img => img.url)
this.$emit('change', this.images)
},
clear() {
this.images = []
this.previewList = []
this.$emit('change', [])
},
setImages(urls) {
this.images = urls.map(url => ({ url }))
this.previewList = this.images.map(img => img.url)
this.$emit('change', this.images)
}
}
}
</script>
<style lang="scss" scoped>
.upload-image-container {
display: flex;
flex-wrap: wrap;
align-items: center;
.image-preview {
position: relative;
margin-right: 10px;
margin-bottom: 10px;
.delete-icon {
position: absolute;
top: -10px;
right: 0;
color: #f56c6c;
font-size: 18px;
cursor: pointer;
background: white;
border-radius: 50%;
padding: 2px;
}
}
.upload-button {
width: 100px;
height: 100px;
line-height: 100px;
text-align: center;
border: 1px dashed #d9d9d9;
border-radius: 6px;
cursor: pointer;
&:hover {
border-color: #409EFF;
}
i {
font-size: 28px;
color: #8c939d;
}
}
}
</style>