22
22
v-if =" !disabled"
23
23
>
24
24
<!-- 上传按钮 -->
25
- <el-button
26
- size =" medium"
27
- type =" primary"
28
- >{{btnText}}</el-button >
25
+ <el-button size =" medium" type =" primary" >{{ btnText }}</el-button >
29
26
<!-- 上传提示 -->
30
- <div
31
- class =" el-upload__tip"
32
- slot =" tip"
33
- v-if =" showTip"
34
- >
27
+ <div class =" el-upload__tip" slot =" tip" v-if =" showTip" >
35
28
请上传
36
29
<template v-if =" fileSize " >
37
30
大小不超过
38
- <b style =" color : #F56C6C " >{{fileSize}}MB</b >
31
+ <b style =" color : #f56c6c ; " >{{ fileSize }}MB</b >
39
32
</template >
40
33
<template v-if =" fileType " >
41
34
格式为
42
- <b style =" color : #F56C6C " >{{fileType.join('/') }}</b >
35
+ <b style =" color : #f56c6c ; " >{{ fileType.join("/") }}</b >
43
36
</template >
44
37
的文件
45
38
</div >
59
52
</template >
60
53
61
54
<script >
62
- import EleUploadList from ' ./EleUploadList'
55
+ import EleUploadList from " ./EleUploadList" ;
63
56
export default {
64
- name: ' EleUploadFile' ,
57
+ name: " EleUploadFile" ,
65
58
components: {
66
- EleUploadList
59
+ EleUploadList,
67
60
},
68
61
props: {
69
62
// 值
@@ -72,7 +65,7 @@ export default {
72
65
// 同 element-ui upload 组件
73
66
action: {
74
67
type: String ,
75
- required: true
68
+ required: true ,
76
69
},
77
70
// 大小限制(MB)
78
71
fileSize: Number ,
@@ -87,32 +80,37 @@ export default {
87
80
// 是否显示文件大小
88
81
isShowSize: {
89
82
type: Boolean ,
90
- default: true
83
+ default: true ,
91
84
},
92
85
// 是否显示下载
93
86
isCanDownload: {
94
87
type: Boolean ,
95
- default: true
88
+ default: true ,
96
89
},
97
90
// 是否可删除
98
91
isCanDelete: {
99
92
type: Boolean ,
100
- default: true
93
+ default: true ,
101
94
},
102
95
// 是否显示删除确认弹窗
103
96
isShowDeleteConfim: {
104
97
type: Boolean ,
105
- default: true
98
+ default: true ,
106
99
},
107
100
// 是否可上传相同文件
108
101
isCanUploadSame: {
109
102
type: Boolean ,
110
- default: true
103
+ default: true ,
111
104
},
112
105
// 是否显示提示
113
106
isShowTip: {
114
107
type: Boolean ,
115
- default: true
108
+ default: true ,
109
+ },
110
+ // 是否显示上传成功的提示
111
+ isShowSuccessTip: {
112
+ type: Boolean ,
113
+ default: true ,
116
114
},
117
115
// 设置上传的请求头部
118
116
// 同 element-ui upload 组件
@@ -121,7 +119,7 @@ export default {
121
119
// 同 element-ui upload 组件
122
120
multiple: {
123
121
type: Boolean ,
124
- default: true
122
+ default: true ,
125
123
},
126
124
// 上传时附带的额外参数
127
125
// 同 element-ui upload 组件
@@ -130,147 +128,173 @@ export default {
130
128
// 同 element-ui upload 组件
131
129
name: {
132
130
type: String ,
133
- default: ' file'
131
+ default: " file" ,
134
132
},
135
133
// 支持发送 cookie 凭证信息
136
134
// 同 element-ui upload 组件
137
135
withCredentials: {
138
136
type: Boolean ,
139
- default: false
137
+ default: false ,
140
138
},
141
139
// 接受上传的文件类型
142
140
// 同 element-ui upload 组件
143
141
accept: String ,
144
142
// 最大允许上传个数
145
143
// 同 element-ui upload 组件
146
- limit: Number
144
+ limit: Number ,
145
+ // 删除前的操作
146
+ // 同 element-ui upload 组件
147
+ beforeRemove: Function ,
147
148
},
148
- data () {
149
+ data () {
149
150
return {
150
- fileList: []
151
- }
151
+ fileList: [],
152
+ };
152
153
},
153
154
computed: {
154
155
// 按钮文本
155
- btnText () {
156
+ btnText () {
156
157
if (this .placeholder ) {
157
- return this .placeholder
158
+ return this .placeholder ;
158
159
} else {
159
160
if (this .multiple ) {
160
- return ' 上传文件'
161
+ return " 上传文件" ;
161
162
} else {
162
- return ' 上传单个文件'
163
+ return " 上传单个文件" ;
163
164
}
164
165
}
165
166
},
166
167
// 是否显示提示
167
- showTip () {
168
- return this .isShowTip && (this .fileType || this .fileSize )
168
+ showTip () {
169
+ return this .isShowTip && (this .fileType || this .fileSize );
169
170
},
170
171
// 列表
171
- list () {
172
- let temp = 1
172
+ list () {
173
+ let temp = 1 ;
173
174
if (this .value ) {
174
175
// 首先将值转为数组
175
- const list = Array .isArray (this .value ) ? this .value : [this .value ]
176
+ const list = Array .isArray (this .value ) ? this .value : [this .value ];
176
177
// 然后将数组转为对象数组
177
178
return list .map ((item ) => {
178
- if (typeof item === ' string' ) {
179
- item = { name: item, url: item }
179
+ if (typeof item === " string" ) {
180
+ item = { name: item, url: item };
180
181
}
181
- item .uid = item .uid || new Date ().getTime () + temp++
182
- return item
183
- })
182
+ item .uid = item .uid || new Date ().getTime () + temp++ ;
183
+ return item;
184
+ });
184
185
} else {
185
- return []
186
+ return [];
186
187
}
187
- }
188
+ },
188
189
},
189
190
methods: {
190
191
// 文件改变
191
- handleChange (file , fileList ) {
192
- this .fileList = fileList
192
+ handleChange (file , fileList ) {
193
+ this .fileList = fileList;
193
194
},
194
195
// 上传前校检格式和大小
195
- handleBeforeUpload (file ) {
196
+ handleBeforeUpload (file ) {
196
197
// 校检文件类型
197
198
if (this .fileType ) {
198
- let fileExtension = ' '
199
- if (file .name .lastIndexOf (' . ' ) > - 1 ) {
200
- fileExtension = file .name .slice (file .name .lastIndexOf (' . ' ) + 1 )
199
+ let fileExtension = " " ;
200
+ if (file .name .lastIndexOf (" . " ) > - 1 ) {
201
+ fileExtension = file .name .slice (file .name .lastIndexOf (" . " ) + 1 );
201
202
}
202
203
const isTypeOk = this .fileType .some ((type ) => {
203
- if (file .type .indexOf (type) > - 1 ) return true
204
- if (fileExtension && fileExtension .indexOf (type) > - 1 ) return true
205
- return false
206
- })
204
+ if (file .type .indexOf (type) > - 1 ) return true ;
205
+ if (fileExtension && fileExtension .indexOf (type) > - 1 ) return true ;
206
+ return false ;
207
+ });
207
208
208
209
if (! isTypeOk) {
209
- this .$message .error (` 文件格式不正确, 请上传${ this .fileType .join (' /' )} 格式文件!` )
210
- return false
210
+ this .$message .error (
211
+ ` 文件格式不正确, 请上传${ this .fileType .join (" /" )} 格式文件!`
212
+ );
213
+ return false ;
211
214
}
212
215
}
213
216
214
217
// 校检文件大小
215
218
if (this .fileSize ) {
216
- const isLt = file .size / 1024 / 1024 < this .fileSize
219
+ const isLt = file .size / 1024 / 1024 < this .fileSize ;
217
220
if (! isLt) {
218
- this .$message .error (` 上传文件大小不能超过 ${ this .fileSize } MB!` )
219
- return false
221
+ this .$message .error (` 上传文件大小不能超过 ${ this .fileSize } MB!` );
222
+ return false ;
220
223
}
221
224
}
222
225
223
226
// 校检相同文件
224
227
if (! this .isCanUploadSame ) {
225
- const isSame = this .list .some ((item ) => item .name + item .size === file .name + file .size )
228
+ const isSame = this .list .some (
229
+ (item ) => item .name + item .size === file .name + file .size
230
+ );
226
231
if (isSame) {
227
- this .$message .error (` 此文件已上传!` )
228
- return false
232
+ this .$message .error (` 此文件已上传!` );
233
+ return false ;
229
234
}
230
235
}
231
- return true
236
+ return true ;
232
237
},
233
238
// 文件个数超出
234
- handleExceed () {
235
- this .$message .error (` 最多上传${ this .limit } 个文件` )
239
+ handleExceed () {
240
+ this .$message .error (` 最多上传${ this .limit } 个文件` );
236
241
},
237
242
// 上传失败
238
- handleUploadError (err ) {
239
- this .$message .error (' 上传失败, 请重试' )
240
- this .$emit (' error' , err)
243
+ handleUploadError (err ) {
244
+ this .$message .error (" 上传失败, 请重试" );
245
+ this .$emit (" error" , err);
241
246
},
242
247
// 上传成功回调
243
- handleUploadSuccess (response , file ) {
244
- this .$message .success (' 上传成功' )
248
+ handleUploadSuccess (response , file ) {
249
+ if (this .isShowSuccessTip ) {
250
+ this .$message .success (" 上传成功" );
251
+ }
245
252
if (this .responseFn ) {
246
- response = this .responseFn (response, file, this .list )
253
+ response = this .responseFn (response, file, this .list );
247
254
}
248
255
if (this .multiple ) {
249
- this .$emit (' input' , [... this .list , response])
256
+ this .$emit (" input" , [... this .list , response]);
250
257
} else {
251
- this .$emit (' input' , response)
258
+ this .$emit (" input" , response);
252
259
}
253
260
254
261
// 上传成功
255
- this .$emit (' success' , response, this .list )
262
+ this .$emit (" success" , response, this .list );
263
+ },
264
+ handleRemove (index ) {
265
+ if (! this .beforeRemove ) {
266
+ this .doRemove (index);
267
+ } else if (typeof this .beforeRemove === " function" ) {
268
+ const before = this .beforeRemove (this .list [index], this .list );
269
+ if (before && before .then ) {
270
+ before .then (
271
+ () => {
272
+ this .doRemove (index);
273
+ },
274
+ () => {}
275
+ );
276
+ } else if (before !== false ) {
277
+ this .doRemove (index);
278
+ }
279
+ }
256
280
},
257
281
// 删除
258
- handleRemove (index ) {
259
- this .$emit (' remove' , this .list [index], this .list )
260
- this .fileList .splice (index, 1 )
282
+ doRemove (index ) {
283
+ this .$emit (" remove" , this .list [index], this .list );
284
+ this .fileList .splice (index, 1 );
261
285
if (this .multiple ) {
262
- const data = [... this .list ]
263
- data .splice (index, 1 )
264
- this .$emit (' input' , data || [])
286
+ const data = [... this .list ];
287
+ data .splice (index, 1 );
288
+ this .$emit (" input" , data || []);
265
289
} else {
266
- this .$emit (' input' , null )
290
+ this .$emit (" input" , null );
267
291
}
268
- }
292
+ },
269
293
},
270
- created () {
271
- this .fileList = this .list
272
- }
273
- }
294
+ created () {
295
+ this .fileList = this .list ;
296
+ },
297
+ };
274
298
</script >
275
299
276
300
<style >
0 commit comments