Skip to content

Commit 61589cb

Browse files
author
Hagan
committed
feat(upload): add httpRequest props
1 parent cfb01dd commit 61589cb

2 files changed

Lines changed: 59 additions & 6 deletions

File tree

src/components/upload/index.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,44 @@ ajax文件上传
423423
</script>
424424
```
425425

426+
## 自定义上传方法
427+
如果组件自身的上传不符合业务场景需求,那么也可以自定义http-request方法,返回一个Promise,用来覆盖默认的上传行为
428+
429+
```html
430+
<c-upload
431+
@success="onSuccess"
432+
@error="onError"
433+
:http-request="httpRequest"
434+
/>
435+
<script>
436+
export default {
437+
methods: {
438+
onSuccess (res, rawFile) {
439+
this.$notify({
440+
title: '上传成功!',
441+
message: '上传成功后的回调函数',
442+
type: 'success'
443+
})
444+
},
445+
onError (err, rawFile) {
446+
this.$notify({
447+
title: '上传失败!',
448+
message: '上传失败后的回调函数',
449+
type: 'error'
450+
})
451+
},
452+
httpRequest (opt) {
453+
return new Promise((resolve, reject) => {
454+
setTimeout(() => {
455+
resolve('完成')
456+
},1000)
457+
})
458+
}
459+
}
460+
}
461+
</script>
462+
```
463+
426464
### 属性
427465

428466
| 名称 | 类型 | 默认值 | 说明 |
@@ -436,6 +474,7 @@ ajax文件上传
436474
| headers | Object | { } | 自定义请求头 |
437475
| data | Object | { } | 上传时附带的额外参数 |
438476
| validator | Function | - | 上传文件之前会执行validator函数进行一些校验,参数会拿到对应的文件对象,若函数返回 false,则代表校验失败,组件会停止该次上传操作 参数为(file: Object) |
477+
| httpRequest | Function | - | 覆盖默认的上传行为,可以自定义上传的实现 |
439478

440479
### 事件
441480

src/components/upload/index.vue

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<script>
2828
import './index.css'
2929
import ajax from './ajax'
30+
let uploadFileCount = 0
3031
3132
export default {
3233
name: 'c-upload',
@@ -70,7 +71,8 @@ export default {
7071
default () {
7172
return {}
7273
}
73-
}
74+
},
75+
httpRequest: Function
7476
},
7577
7678
data () {
@@ -129,30 +131,42 @@ export default {
129131
if (isValid) this.post(rawFile)
130132
},
131133
132-
post (rawFile) {
134+
async post (rawFile) {
135+
this.loading = true
136+
uploadFileCount++
133137
const { fid } = rawFile
134138
const options = {
135139
headers: this.headers,
136-
// withCredentials: this.withCredentials,
140+
withCredentials: this.withCredentials,
137141
file: rawFile,
138142
data: this.data,
139143
filename: this.name,
140144
action: this.action,
141145
onProgress: (e) => {
142-
if (!this.loading) this.loading = true
143146
this.$emit('progress', e, rawFile)
144147
},
145148
onSuccess: (res) => {
146149
this.$emit('success', res, rawFile)
147150
delete this.reqs[fid]
148-
this.loading = false
151+
uploadFileCount--
152+
if (uploadFileCount === 0) this.loading = false
149153
},
150154
onError: (err) => {
151155
this.$emit('error', err, rawFile)
152156
delete this.reqs[fid]
153-
this.loading = false
157+
uploadFileCount--
158+
if (uploadFileCount === 0) this.loading = false
154159
}
155160
}
161+
if (this.httpRequest instanceof Function) {
162+
const {headers, file, data, filename, action} = options
163+
this.reqs[fid] = this.httpRequest({
164+
headers, file, data, filename, action
165+
})
166+
.then(options.onSuccess)
167+
.catch(options.onError)
168+
return
169+
}
156170
const req = ajax(options)
157171
this.reqs[fid] = req
158172
},

0 commit comments

Comments
 (0)