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
30
31
32
|
import ajax from './ajax';
import { oneOf } from '../../utils/assist';
const prefixCls = 'ivu-upload';
|
3affd6f3
梁灏
init upload compo...
|
33
|
export default {
|
34ee7b4a
梁灏
support Tree & ad...
|
34
|
name: 'Upload',
|
ddef2bb3
梁灏
update Upload
|
35
|
components: { UploadList },
|
9671827f
梁灏
update Upload
|
36
37
38
39
40
41
42
43
|
props: {
action: {
type: String,
required: true
},
headers: {
type: Object,
default () {
|
7f0b9f3a
梁灏
update Upload
|
44
|
return {};
|
9671827f
梁灏
update Upload
|
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
|
}
},
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
|
70
71
|
},
default: 'select'
|
9671827f
梁灏
update Upload
|
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
|
},
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...
|
135
|
data () {
|
9671827f
梁灏
update Upload
|
136
137
138
139
140
141
142
143
144
145
146
147
|
return {
prefixCls: prefixCls,
dragOver: false,
fileList: [],
tempIndex: 1
};
},
computed: {
classes () {
return [
`${prefixCls}`,
{
|
ddef2bb3
梁灏
update Upload
|
148
|
[`${prefixCls}-select`]: this.type === 'select',
|
9671827f
梁灏
update Upload
|
149
|
[`${prefixCls}-drag`]: this.type === 'drag',
|
bc3bcb09
梁灏
update Upload
|
150
|
[`${prefixCls}-dragOver`]: this.type === 'drag' && this.dragOver
|
9671827f
梁灏
update Upload
|
151
152
153
154
155
156
157
|
}
];
},
},
methods: {
handleClick () {
|
5d08ddf2
梁灏
support Progress ...
|
158
|
this.$refs.input.click();
|
9671827f
梁灏
update Upload
|
159
160
161
162
163
164
165
166
|
},
handleChange (e) {
const files = e.target.files;
if (!files) {
return;
}
this.uploadFiles(files);
|
5d08ddf2
梁灏
support Progress ...
|
167
|
this.$refs.input.value = null;
|
9671827f
梁灏
update Upload
|
168
169
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
|
},
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
|
206
207
208
209
210
|
// 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
|
211
|
this.onFormatError(file, this.fileList);
|
a6ac0a76
梁灏
update Upload
|
212
213
214
215
216
217
218
|
return false;
}
}
// check maxSize
if (this.maxSize) {
if (file.size > this.maxSize * 1024) {
|
3c2f68f2
梁灏
update Upload
|
219
|
this.onExceededSize(file, this.fileList);
|
a6ac0a76
梁灏
update Upload
|
220
221
222
223
|
return false;
}
}
|
9671827f
梁灏
update Upload
|
224
225
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
|
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
|
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
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;
|
5d08ddf2
梁灏
support Progress ...
|
280
281
|
// todo 事件
// this.$dispatch('on-form-change', _file);
|
9671827f
梁灏
update Upload
|
282
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
|
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...
|
325
|
},
|
3affd6f3
梁灏
init upload compo...
|
326
327
|
};
</script>
|