-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathCustomRequest.js
55 lines (46 loc) · 1.3 KB
/
CustomRequest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import React, { Component } from 'react'
import RUG from 'react-upload-gallery'
import axios from 'axios'
class App extends Component {
customRequest({ uid, file, send, action, headers, onProgress, onSuccess, onError }) {
const form = new FormData();
// send file
form.append('file', file)
const CancelToken = axios.CancelToken
const source = CancelToken.source()
axios.post(
action,
form,
{
headers: {
'x-access-token' : 'xxxx'
},
onUploadProgress: ({ total, loaded }) => {
onProgress(uid, Math.round(loaded / total * 100));
},
cancelToken: source.token
}
).then(({ data: response }) => {
onSuccess(uid, response);
})
.catch(error => {
onError(uid, {
action,
status: error.request,
response: error.response
})
})
return {
abort() {
source.cancel()
}
}
}
render() {
return <RUG
action="/api/upload"
customRequest={this.customRequest}
/>
}
}
export default App