index.vue
2.31 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
<template>
<div class="upload-wrapper">
<label>{{ placeholderCopy }}</label>
<div class="uw-right">
<el-button class="uw-btn">
<slot></slot>
</el-button>
<input
class="uw-input"
type="file"
id="image_uploads"
name="image_uploads"
:accept="accept"
:multiple="multiple"
@change="handleChange"
/>
</div>
</div>
</template>
<script>
export default {
props: {
placeholder: {
type: String,
default: '上传图片,可多选'
},
accept: {
type: String,
default: '.jpg, .jpeg, .png, .JPG, .JPEG, .PNG, .bmp, .BMP'
},
multiple: {
type: Boolean,
default: false
},
onExceed: {
type: Function,
default: () => {}
},
limit: {
type: Number
},
onChange: {
type: Function,
default: () => {}
}
},
data() {
return {
placeholderCopy: this.placeholder
}
},
methods: {
handleChange(e) {
const files = e.currentTarget.files
const length = files.length
if (!this.isValidator(files)) {
this.$emit('onFail', files)
return
}
if (this.limit && length > this.limit) {
this.onExceed()
return
}
if (files.length > 0) {
this.placeholderCopy = files[0].name
this.onChange(Array.from(files))
}
},
getAcceptType() {
const arr = this.accept
.toLowerCase()
.split(',')
.map(item => `image/${item.slice(item.indexOf('.') + 1)}`)
return Array.from(new Set(arr))
},
isValidator(files) {
return Array.from(files).every(file =>
this.getAcceptType().includes(file.type)
)
}
},
mounted() {
this.getAcceptType()
}
}
</script>
<style lang="scss" scoped>
.upload-wrapper {
width: 300px;
height: 42px;
background: #f2f2f2;
display: inline-block;
position: relative;
label {
font-size: 12px;
line-height: 42px;
width: 100%;
display: inline-block;
height: 100%;
overflow: hidden;
}
.uw-right {
position: absolute;
top: 0;
bottom: 0;
right: 0;
width: 70px;
}
input {
position: absolute;
top: 0;
bottom: 0;
right: 0;
width: 100%;
height: 100%;
opacity: 0;
cursor: pointer;
}
}
</style>