1
1
<template >
2
2
<div class =" py-upload" >
3
- <div class =" py-upload__item"
3
+ <div
4
+ class =" py-upload__item"
4
5
:class =" drapClass"
5
6
@click.stop =" inputClick"
6
- @dragover.prevent =" drapFile = true"
7
- @dragleave.prevent =" drapFile = false"
8
- @drop.prevent =" onDrop" >
9
- <input
10
- type =" file"
11
- ref =" input"
12
- :multiple =" multiple"
13
- :accept =" accept"
14
- @change =" fileChange" >
15
- <slot ></slot >
7
+ @dragover.prevent =" drapFile = true;"
8
+ @dragleave.prevent =" drapFile = false;"
9
+ @drop.prevent =" onDrop"
10
+ >
11
+ <input type =" file" ref =" input" :multiple =" multiple" :accept =" accept" @change =" fileChange" />
12
+ <slot ></slot >
16
13
</div >
17
- <uploadList @on-remove =" onRemove"
18
- @onItem =" onItem"
14
+ <uploadList
15
+ @on-remove =" onRemove"
16
+ :onClickItem =" onClickItem"
19
17
:on-before-remove =" onbeforeRemove"
20
- :files =" fileList" />
18
+ :files =" fileList"
19
+ />
21
20
</div >
22
21
</template >
23
22
@@ -56,6 +55,8 @@ export default {
56
55
return Item;
57
56
});
58
57
this .fileList = this .value ;
58
+ } else {
59
+ this .fileList = [];
59
60
}
60
61
},
61
62
},
@@ -77,10 +78,6 @@ export default {
77
78
this .dragOver = false ;
78
79
this .fileChange (e .dataTransfer .files );
79
80
},
80
- // 点击列表中的文件
81
- onItem (index , item ) {
82
- this .$emit (' on-item' , index, item, this .fileList );
83
- },
84
81
// 删除fileList文件
85
82
onRemove (item , index ) {
86
83
this .fileList .splice (index, 1 );
@@ -92,6 +89,7 @@ export default {
92
89
if (! Files) {
93
90
return false ;
94
91
}
92
+ this .drapFile = false ;
95
93
for (let i = 0 ; i < Files .length ; i += 1 ) {
96
94
const File = {
97
95
status: ' status' ,
@@ -109,8 +107,10 @@ export default {
109
107
},
110
108
// 选择上传的文件
111
109
fileSelect (file , fileList ) {
112
- this .fileFormat (file, fileList);
113
- this .$emit (' before-select' , file, fileList);
110
+ if (this .beforeSelect (file, fileList) === false ) {
111
+ return false ;
112
+ }
113
+ return this .fileFormat (file, fileList);
114
114
},
115
115
// 验证文件后缀格式
116
116
fileFormat (file , fileList ) {
@@ -119,9 +119,9 @@ export default {
119
119
return false ;
120
120
}
121
121
const name = file .name ? file .name .split (' .' ) : [];
122
- const fileType = name[name .length ];
122
+ const fileType = name[name .length - 1 ];
123
123
if (this .format .indexOf (fileType) === - 1 ) {
124
- this .$emit ( ' on-format-err ' , file, fileList);
124
+ this .onFormatErr ( file, fileList);
125
125
return false ;
126
126
}
127
127
return this .fileMaxSize (this .format , file, fileList);
@@ -130,7 +130,7 @@ export default {
130
130
fileMaxSize (file , fileList ) {
131
131
if (this .maxSize !== undefined ) {
132
132
if (file .size > this .maxSize * 1024 ) {
133
- this .$emit ( ' on-size-err ' , this . maxSize , file, fileList);
133
+ this .onSizeErr ( file, fileList);
134
134
return false ;
135
135
}
136
136
}
@@ -141,12 +141,31 @@ export default {
141
141
if (! this .onBeforeUpload ) {
142
142
return this .fileStart (file);
143
143
}
144
+ const beforeFn = this .onBeforeUpload (file);
145
+ if (beforeFn && beforeFn .then ) {
146
+ beforeFn .then (
147
+ processedFile => {
148
+ if (Object .prototype .toString .call (processedFile) === ' [object File]' ) {
149
+ this .fileStart (processedFile, fileList);
150
+ } else {
151
+ this .fileStart (file, fileList);
152
+ }
153
+ },
154
+ err => {
155
+ this .handleError (err, file);
156
+ },
157
+ );
158
+ } else if (beforeFn !== false ) {
159
+ this .fileStart (file, fileList);
160
+ } else {
161
+ this .handleError (' ' , file);
162
+ }
144
163
return this .fileStart (file, fileList);
145
164
},
146
165
// 开始上传文件
147
166
fileStart (file , fileList ) {
148
167
if (! this .action ) {
149
- this .$emit ( ' on-error ' , ' 上传地址必填! ' , file, fileList);
168
+ this .onError ( ' 上传地址必填' , file, fileList);
150
169
return false ;
151
170
}
152
171
const File = this .getFile (file);
@@ -186,7 +205,7 @@ export default {
186
205
handleProgress (e , file ) {
187
206
const File = file;
188
207
File .status = ' progress' ;
189
- File .percentage = parseInt (e .percent , 10 ) || 0 ;
208
+ File .percentage = parseInt (e .percent , 10 ) || 1 ;
190
209
this .onProgress (e, File , this .fileList );
191
210
},
192
211
// 上传成功回调response
@@ -199,21 +218,31 @@ export default {
199
218
setTimeout (() => {
200
219
File .showProgress = false ;
201
220
}, 1000 );
202
- this .$emit (' on-success' , res, File );
203
221
},
204
222
// 上传失败
205
223
handleError (err , file ) {
206
224
const File = file;
207
225
File .status = ' fail' ;
208
226
File .percentage = 100 ;
209
227
this .onError (err, File , this .fileList );
210
- this .$emit (' on-error' , err, File );
211
228
},
212
229
},
213
230
props: {
214
231
accept: [String ],
215
232
maxSize: [Number ],
216
233
action: [String ],
234
+ beforeSelect: {
235
+ type: Function ,
236
+ default : () => {},
237
+ },
238
+ onSizeErr: {
239
+ type: Function ,
240
+ default : () => {},
241
+ },
242
+ onFormatErr: {
243
+ type: Function ,
244
+ default : () => {},
245
+ },
217
246
onProgress: {
218
247
type: Function ,
219
248
default : () => {},
@@ -232,13 +261,20 @@ export default {
232
261
},
233
262
type: {
234
263
type: String ,
235
- default: ' file ' ,
264
+ default: ' select ' ,
236
265
},
237
266
onbeforeRemove: {
238
267
type: Function ,
239
268
default : () => {},
240
269
},
241
- onBeforeUpload: Function ,
270
+ onClickItem: {
271
+ type: Function ,
272
+ default : () => {},
273
+ },
274
+ onBeforeUpload: {
275
+ type: Function ,
276
+ default : () => {},
277
+ },
242
278
data: [Object ],
243
279
format: {
244
280
type: Array ,
0 commit comments