| 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185 |
| <!-- -->
<template>
<div class="uploader">
<GButton icon="upload" @click="onClickUpload">
<slot></slot>
</GButton>
<slot name="tips"></slot>
<div class="imgItem" v-for="(item,index) in fileList" :key="index">
<div>
<Icon class="loading" icon="loading" v-if="item.loading" />
<img v-else width="50" :src="item.url" alt />
<span :class="{error:status=== 'error'}">{{item.name}}</span>
</div>
<span @click="removeFile(index)">
<Icon class="delete" icon="delete" />
</span>
</div>
<div ref="input" style="width:0;height:0;overflow:hidden;"></div>
</div>
</template>
<script>
import Icon from './icon'
import GButton from './button/button'
export default {
name: 'uploader',
components: {
Icon,
GButton
},
data() {
return {
src: '',
status: 'success'
}
},
props: {
name: {
type: String,
required: true
},
action: {
type: String,
required: true
},
method: {
type: String,
default: 'POST'
},
parseResponse: {
type: Function,
required: true
},
beforeUpload: {
type: Function
},
fileList: {
type: Array,
default: () => []
}
},
methods: {
removeFile(index) {
let list = [...this.fileList]
list.splice(index, 1)
this.$emit('update:fileList', list)
},
onClickUpload() {
const input = this.createInput()
input.addEventListener('change', e => {
this.updateFile(input.files)
// this.updateFiles(input.files)
input.remove()
})
input.click()
},
createInput() {
this.$refs.input.innerHTML = ''
const input = document.createElement('input')
input.type = 'file'
input.multiple = 'multiple'
this.$refs.input.appendChild(input)
return input
},
updateFiles(files) {
let formData = new FormData()
for (let i = 0; i < files.length; i++) {
formData.append(this.name, files[i])
}
var xhr = new XMLHttpRequest()
xhr.open(this.method, this.action)
xhr.send(formData)
},
updateFile(files) {
let arr = [...this.fileList]
for (let i = 0; i < files.length; i++) {
let file = files[i]
if (this.beforeUpload) {
let flag = this.beforeUpload(file)
if (!flag) {
return
}
}
file.loading = true
arr.push(file)
}
this.$emit('update:fileList', arr)
for (let i = 0; i < files.length; i++) {
new Promise((resolve) => {
let file = files[i]
const { name, size, type } = file
const index = arr.indexOf(file)
let formData = new FormData()
formData.append(this.name, file)
var xhr = new XMLHttpRequest()
xhr.open(this.method, this.action, true)
xhr.onload = () => {
let url
url = xhr.response
? this.parseResponse(xhr.response)
: ''
//关闭loading
let newArr = [...this.fileList]
newArr[index] = { name, size, type, url }
this.$emit('update:fileList', newArr)
this.$emit('success', xhr.response)
resolve(xhr.response)
}
xhr.onerror = () => {
let newArr = [...this.fileList]
newArr[index] = { name, size, type, status: 'error' }
this.$emit('update:fileList', newArr)
this.error(xhr)
}
xhr.setRequestHeader('If-Modified-Since', '0')
xhr.send(formData)
})
}
},
error(xhr) {
this.status = 'error'
this.$emit('error', xhr)
}
}
}
</script>
<style scoped lang='scss'>
@import './var';
.uploader {
.loading {
@include spin;
width: 32px;
height: 32px;
}
.delete {
cursor: pointer;
margin-left: 50px;
}
.error {
color: red;
}
.imgItem {
border: 1px solid #c0ccda;
display: flex;
align-items: center;
padding: 10px;
border-radius: 5px;
margin-top: 10px;
justify-content: space-between;
> div {
display: flex;
justify-content: center;
align-items: center;
}
img {
width: 70px;
height: 70px;
margin-right: 10px;
border: 1px solid #c0ccda;
}
}
}
</style> |