3affd6f3
梁灏
init upload compo...
|
1
|
<template>
|
ddef2bb3
梁灏
update Upload
|
2
3
4
5
6
|
<div :class="[prefixCls]">
<div
:class="classes"
@click="handleClick"
@drop.prevent="onDrop"
|
929d4915
梁灏
Upload add paste ...
|
7
|
@paste="handlePaste"
|
ddef2bb3
梁灏
update Upload
|
8
9
10
|
@dragover.prevent="dragOver = true"
@dragleave.prevent="dragOver = false">
<input
|
5d08ddf2
梁灏
support Progress ...
|
11
|
ref="input"
|
ddef2bb3
梁灏
update Upload
|
12
|
type="file"
|
5d08ddf2
梁灏
support Progress ...
|
13
|
:class="[prefixCls + '-input']"
|
ddef2bb3
梁灏
update Upload
|
14
15
16
17
18
19
20
21
22
23
24
|
@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
|
25
|
</div>
|
3affd6f3
梁灏
init upload compo...
|
26
27
|
</template>
<script>
|
ddef2bb3
梁灏
update Upload
|
28
|
import UploadList from './upload-list.vue';
|
9671827f
梁灏
update Upload
|
29
30
|
import ajax from './ajax';
import { oneOf } from '../../utils/assist';
|
cd78c9c4
梁灏
some comps suppor...
|
31
|
import Emitter from '../../mixins/emitter';
|
9671827f
梁灏
update Upload
|
32
33
34
|
const prefixCls = 'ivu-upload';
|
3affd6f3
梁灏
init upload compo...
|
35
|
export default {
|
34ee7b4a
梁灏
support Tree & ad...
|
36
|
name: 'Upload',
|
cd78c9c4
梁灏
some comps suppor...
|
37
|
mixins: [ Emitter ],
|
ddef2bb3
梁灏
update Upload
|
38
|
components: { UploadList },
|
9671827f
梁灏
update Upload
|
39
40
41
42
43
44
45
46
|
props: {
action: {
type: String,
required: true
},
headers: {
type: Object,
default () {
|
7f0b9f3a
梁灏
update Upload
|
47
|
return {};
|
9671827f
梁灏
update Upload
|
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
|
}
},
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
|
73
74
|
},
default: 'select'
|
9671827f
梁灏
update Upload
|
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
|
},
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 [];
}
|
929d4915
梁灏
Upload add paste ...
|
136
137
138
139
|
},
paste: {
type: Boolean,
default: false
|
9671827f
梁灏
update Upload
|
140
141
|
}
},
|
3affd6f3
梁灏
init upload compo...
|
142
|
data () {
|
9671827f
梁灏
update Upload
|
143
144
145
146
147
148
149
150
151
152
153
154
|
return {
prefixCls: prefixCls,
dragOver: false,
fileList: [],
tempIndex: 1
};
},
computed: {
classes () {
return [
`${prefixCls}`,
{
|
ddef2bb3
梁灏
update Upload
|
155
|
[`${prefixCls}-select`]: this.type === 'select',
|
9671827f
梁灏
update Upload
|
156
|
[`${prefixCls}-drag`]: this.type === 'drag',
|
bc3bcb09
梁灏
update Upload
|
157
|
[`${prefixCls}-dragOver`]: this.type === 'drag' && this.dragOver
|
9671827f
梁灏
update Upload
|
158
159
160
161
162
163
164
|
}
];
},
},
methods: {
handleClick () {
|
5d08ddf2
梁灏
support Progress ...
|
165
|
this.$refs.input.click();
|
9671827f
梁灏
update Upload
|
166
167
168
169
170
171
172
173
|
},
handleChange (e) {
const files = e.target.files;
if (!files) {
return;
}
this.uploadFiles(files);
|
5d08ddf2
梁灏
support Progress ...
|
174
|
this.$refs.input.value = null;
|
9671827f
梁灏
update Upload
|
175
176
177
178
179
|
},
onDrop (e) {
this.dragOver = false;
this.uploadFiles(e.dataTransfer.files);
},
|
929d4915
梁灏
Upload add paste ...
|
180
181
182
183
184
|
handlePaste (e) {
if (this.paste) {
this.uploadFiles(e.clipboardData.files);
}
},
|
9671827f
梁灏
update Upload
|
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
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
|
218
219
220
221
222
|
// 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
|
223
|
this.onFormatError(file, this.fileList);
|
a6ac0a76
梁灏
update Upload
|
224
225
226
227
228
229
230
|
return false;
}
}
// check maxSize
if (this.maxSize) {
if (file.size > this.maxSize * 1024) {
|
3c2f68f2
梁灏
update Upload
|
231
|
this.onExceededSize(file, this.fileList);
|
a6ac0a76
梁灏
update Upload
|
232
233
234
235
|
return false;
}
}
|
9671827f
梁灏
update Upload
|
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
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
|
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
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;
|
9671827f
梁灏
update Upload
|
292
|
this.onSuccess(res, _file, this.fileList);
|
3f91ed4d
li.fameng
Fix on-form-chang...
|
293
|
this.dispatch('FormItem', 'on-form-change', _file);
|
9671827f
梁灏
update Upload
|
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
326
327
328
329
330
331
332
333
334
335
|
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...
|
336
|
},
|
3affd6f3
梁灏
init upload compo...
|
337
|
};
|
3f91ed4d
li.fameng
Fix on-form-chang...
|
338
|
</script>
|