3affd6f3
梁灏
init upload compo...
|
1
|
<template>
|
ddef2bb3
梁灏
update Upload
|
2
3
4
5
6
7
8
9
|
<div :class="[prefixCls]">
<div
:class="classes"
@click="handleClick"
@drop.prevent="onDrop"
@dragover.prevent="dragOver = true"
@dragleave.prevent="dragOver = false">
<input
|
5d08ddf2
梁灏
support Progress ...
|
10
|
ref="input"
|
ddef2bb3
梁灏
update Upload
|
11
|
type="file"
|
5d08ddf2
梁灏
support Progress ...
|
12
|
:class="[prefixCls + '-input']"
|
ddef2bb3
梁灏
update Upload
|
13
14
15
16
17
18
19
20
21
22
23
|
@change="handleChange"
:multiple="multiple"
:accept="accept">
<slot></slot>
</div>
<slot name="tip"></slot>
<upload-list
v-if="showUploadList"
:files="fileList"
@on-file-remove="handleRemove"
@on-file-preview="handlePreview"></upload-list>
|
9671827f
梁灏
update Upload
|
24
|
</div>
|
3affd6f3
梁灏
init upload compo...
|
25
26
|
</template>
<script>
|
ddef2bb3
梁灏
update Upload
|
27
|
import UploadList from './upload-list.vue';
|
9671827f
梁灏
update Upload
|
28
29
|
import ajax from './ajax';
import { oneOf } from '../../utils/assist';
|
cd78c9c4
梁灏
some comps suppor...
|
30
|
import Emitter from '../../mixins/emitter';
|
9671827f
梁灏
update Upload
|
31
32
33
|
const prefixCls = 'ivu-upload';
|
3affd6f3
梁灏
init upload compo...
|
34
|
export default {
|
34ee7b4a
梁灏
support Tree & ad...
|
35
|
name: 'Upload',
|
cd78c9c4
梁灏
some comps suppor...
|
36
|
mixins: [ Emitter ],
|
ddef2bb3
梁灏
update Upload
|
37
|
components: { UploadList },
|
9671827f
梁灏
update Upload
|
38
39
40
41
42
43
44
45
|
props: {
action: {
type: String,
required: true
},
headers: {
type: Object,
default () {
|
7f0b9f3a
梁灏
update Upload
|
46
|
return {};
|
9671827f
梁灏
update Upload
|
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
|
}
},
multiple: {
type: Boolean,
default: false
},
data: {
type: Object
},
name: {
type: String,
default: 'file'
},
withCredentials: {
type: Boolean,
default: false
},
showUploadList: {
type: Boolean,
default: true
},
type: {
type: String,
validator (value) {
return oneOf(value, ['select', 'drag']);
|
ddef2bb3
梁灏
update Upload
|
72
73
|
},
default: 'select'
|
9671827f
梁灏
update Upload
|
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
|
},
format: {
type: Array,
default () {
return [];
}
},
accept: {
type: String
},
maxSize: {
type: Number
},
beforeUpload: Function,
onProgress: {
type: Function,
default () {
return {};
}
},
onSuccess: {
type: Function,
default () {
return {};
}
},
onError: {
type: Function,
default () {
return {};
}
},
onRemove: {
type: Function,
default () {
return {};
}
},
onPreview: {
type: Function,
default () {
return {};
}
},
onExceededSize: {
type: Function,
default () {
return {};
}
},
onFormatError: {
type: Function,
default () {
return {};
}
},
defaultFileList: {
type: Array,
default() {
return [];
}
}
},
|
3affd6f3
梁灏
init upload compo...
|
137
|
data () {
|
9671827f
梁灏
update Upload
|
138
139
140
141
142
143
144
145
146
147
148
149
|
return {
prefixCls: prefixCls,
dragOver: false,
fileList: [],
tempIndex: 1
};
},
computed: {
classes () {
return [
`${prefixCls}`,
{
|
ddef2bb3
梁灏
update Upload
|
150
|
[`${prefixCls}-select`]: this.type === 'select',
|
9671827f
梁灏
update Upload
|
151
|
[`${prefixCls}-drag`]: this.type === 'drag',
|
bc3bcb09
梁灏
update Upload
|
152
|
[`${prefixCls}-dragOver`]: this.type === 'drag' && this.dragOver
|
9671827f
梁灏
update Upload
|
153
154
155
156
157
158
159
|
}
];
},
},
methods: {
handleClick () {
|
5d08ddf2
梁灏
support Progress ...
|
160
|
this.$refs.input.click();
|
9671827f
梁灏
update Upload
|
161
162
163
164
165
166
167
168
|
},
handleChange (e) {
const files = e.target.files;
if (!files) {
return;
}
this.uploadFiles(files);
|
5d08ddf2
梁灏
support Progress ...
|
169
|
this.$refs.input.value = null;
|
9671827f
梁灏
update Upload
|
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
},
onDrop (e) {
this.dragOver = false;
this.uploadFiles(e.dataTransfer.files);
},
uploadFiles (files) {
let postFiles = Array.prototype.slice.call(files);
if (!this.multiple) postFiles = postFiles.slice(0, 1);
if (postFiles.length === 0) return;
postFiles.forEach(file => {
this.upload(file);
});
},
upload (file) {
if (!this.beforeUpload) {
return this.post(file);
}
const before = this.beforeUpload(file);
if (before && before.then) {
before.then(processedFile => {
if (Object.prototype.toString.call(processedFile) === '[object File]') {
this.post(processedFile);
} else {
this.post(file);
}
}, () => {
// this.$emit('cancel', file);
});
} else if (before !== false) {
this.post(file);
} else {
// this.$emit('cancel', file);
}
},
post (file) {
|
a6ac0a76
梁灏
update Upload
|
208
209
210
211
212
|
// check format
if (this.format.length) {
const _file_format = file.name.split('.').pop().toLocaleLowerCase();
const checked = this.format.some(item => item.toLocaleLowerCase() === _file_format);
if (!checked) {
|
3c2f68f2
梁灏
update Upload
|
213
|
this.onFormatError(file, this.fileList);
|
a6ac0a76
梁灏
update Upload
|
214
215
216
217
218
219
220
|
return false;
}
}
// check maxSize
if (this.maxSize) {
if (file.size > this.maxSize * 1024) {
|
3c2f68f2
梁灏
update Upload
|
221
|
this.onExceededSize(file, this.fileList);
|
a6ac0a76
梁灏
update Upload
|
222
223
224
225
|
return false;
}
}
|
9671827f
梁灏
update Upload
|
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
this.handleStart(file);
let formData = new FormData();
formData.append(this.name, file);
ajax({
headers: this.headers,
withCredentials: this.withCredentials,
file: file,
data: this.data,
filename: this.name,
action: this.action,
onProgress: e => {
this.handleProgress(e, file);
},
onSuccess: res => {
this.handleSuccess(res, file);
},
onError: (err, response) => {
this.handleError(err, response, file);
}
});
},
handleStart (file) {
file.uid = Date.now() + this.tempIndex++;
const _file = {
status: 'uploading',
name: file.name,
size: file.size,
percentage: 0,
uid: file.uid,
showProgress: true
};
|
9671827f
梁灏
update Upload
|
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
this.fileList.push(_file);
},
getFile (file) {
const fileList = this.fileList;
let target;
fileList.every(item => {
target = file.uid === item.uid ? item : null;
return !target;
});
return target;
},
handleProgress (e, file) {
const _file = this.getFile(file);
this.onProgress(e, _file, this.fileList);
_file.percentage = e.percent || 0;
},
handleSuccess (res, file) {
const _file = this.getFile(file);
if (_file) {
_file.status = 'finished';
_file.response = res;
|
cd78c9c4
梁灏
some comps suppor...
|
282
|
this.dispatch('FormItem', 'on-form-change', _file);
|
9671827f
梁灏
update Upload
|
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
this.onSuccess(res, _file, this.fileList);
setTimeout(() => {
_file.showProgress = false;
}, 1000);
}
},
handleError (err, response, file) {
const _file = this.getFile(file);
const fileList = this.fileList;
_file.status = 'fail';
fileList.splice(fileList.indexOf(_file), 1);
this.onError(err, response, file);
},
handleRemove(file) {
const fileList = this.fileList;
fileList.splice(fileList.indexOf(file), 1);
this.onRemove(file, fileList);
},
handlePreview(file) {
if (file.status === 'finished') {
this.onPreview(file);
}
},
clearFiles() {
this.fileList = [];
}
},
watch: {
defaultFileList: {
immediate: true,
handler(fileList) {
this.fileList = fileList.map(item => {
item.status = 'finished';
item.percentage = 100;
item.uid = Date.now() + this.tempIndex++;
return item;
});
}
}
|
3affd6f3
梁灏
init upload compo...
|
326
|
},
|
3affd6f3
梁灏
init upload compo...
|
327
328
|
};
</script>
|