-
-
Notifications
You must be signed in to change notification settings - Fork 320
/
Copy pathAjaxUploader.jsx
245 lines (223 loc) · 5.72 KB
/
AjaxUploader.jsx
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* eslint react/no-is-mounted:0 react/sort-comp:0 */
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import defaultRequest from './request';
import getUid from './uid';
import attrAccept from './attr-accept';
import traverseFileTree from './traverseFileTree';
class AjaxUploader extends Component {
static propTypes = {
id: PropTypes.string,
component: PropTypes.string,
style: PropTypes.object,
prefixCls: PropTypes.string,
className: PropTypes.string,
multiple: PropTypes.bool,
directory: PropTypes.bool,
disabled: PropTypes.bool,
accept: PropTypes.string,
children: PropTypes.any,
onStart: PropTypes.func,
data: PropTypes.oneOfType([
PropTypes.object,
PropTypes.func,
]),
action: PropTypes.oneOfType([
PropTypes.string,
PropTypes.func,
]),
headers: PropTypes.object,
beforeUpload: PropTypes.func,
customRequest: PropTypes.func,
onProgress: PropTypes.func,
withCredentials: PropTypes.bool,
openFileDialogOnClick: PropTypes.bool,
}
state = { uid: getUid() }
reqs = {}
onChange = e => {
const files = e.target.files;
this.uploadFiles(files);
this.reset();
}
onClick = () => {
const el = this.fileInput;
if (!el) {
return;
}
el.click();
}
onKeyDown = e => {
if (e.key === 'Enter') {
this.onClick();
}
}
onFileDrop = e => {
e.preventDefault();
if (e.type === 'dragover') {
return;
}
if (this.props.directory) {
traverseFileTree(
e.dataTransfer.items,
this.uploadFiles,
_file => attrAccept(_file, this.props.accept)
);
} else {
const files = Array.prototype.slice.call(e.dataTransfer.files).filter(
file => attrAccept(file, this.props.accept)
);
this.uploadFiles(files);
}
}
componentDidMount() {
this._isMounted = true;
}
componentWillUnmount() {
this._isMounted = false;
this.abort();
}
uploadFiles = (files) => {
const postFiles = Array.prototype.slice.call(files);
postFiles
.map(file => {
file.uid = getUid();
return file;
})
.forEach(file => {
this.upload(file, postFiles);
});
};
upload(file, fileList) {
const { props } = this;
if (!props.beforeUpload) {
// always async in case use react state to keep fileList
return setTimeout(() => this.post(file), 0);
}
const before = props.beforeUpload(file, fileList);
if (before && before.then) {
before.then((processedFile) => {
const processedFileType = Object.prototype.toString.call(processedFile);
if (processedFileType === '[object File]' || processedFileType === '[object Blob]') {
return this.post(processedFile);
}
return this.post(file);
}).catch(e => {
console && console.log(e); // eslint-disable-line
});
} else if (before !== false) {
setTimeout(() => this.post(file), 0);
}
}
post(file) {
if (!this._isMounted) {
return;
}
const { props } = this;
let { data } = props;
const { onStart, onProgress } = props;
if (typeof data === 'function') {
data = data(file);
}
new Promise(resolve => {
const { action } = props;
if (typeof action === 'function') {
return resolve(action(file));
}
resolve(action);
}).then(action => {
const { uid } = file;
const request = props.customRequest || defaultRequest;
this.reqs[uid] = request({
action,
filename: props.name,
file,
data,
headers: props.headers,
withCredentials: props.withCredentials,
onProgress: onProgress ? e => {
onProgress(e, file);
} : null,
onSuccess: (ret, xhr) => {
delete this.reqs[uid];
props.onSuccess(ret, file, xhr);
},
onError: (err, ret) => {
delete this.reqs[uid];
props.onError(err, ret, file);
},
});
onStart(file);
});
}
reset() {
this.setState({
uid: getUid(),
});
}
abort(file) {
const { reqs } = this;
if (file) {
let uid = file;
if (file && file.uid) {
uid = file.uid;
}
if (reqs[uid]) {
reqs[uid].abort();
delete reqs[uid];
}
} else {
Object.keys(reqs).forEach((uid) => {
if (reqs[uid] && reqs[uid].abort) {
reqs[uid].abort();
}
delete reqs[uid];
});
}
}
saveFileInput = (node) => {
this.fileInput = node;
}
render() {
const {
component: Tag, prefixCls, className, disabled, id,
style, multiple, accept, children, directory, openFileDialogOnClick,
} = this.props;
const cls = classNames({
[prefixCls]: true,
[`${prefixCls}-disabled`]: disabled,
[className]: className,
});
const events = disabled ? {} : {
onClick: openFileDialogOnClick ? this.onClick : () => { },
onKeyDown: openFileDialogOnClick ? this.onKeyDown : () => { },
onDrop: this.onFileDrop,
onDragOver: this.onFileDrop,
tabIndex: '0',
};
return (
<Tag
{...events}
className={cls}
role="button"
style={style}
>
<input
id={id}
type="file"
ref={this.saveFileInput}
key={this.state.uid}
style={{ display: 'none' }}
accept={accept}
directory={directory ? 'directory' : null}
webkitdirectory={directory ? 'webkitdirectory' : null}
multiple={multiple}
onChange={this.onChange}
/>
{children}
</Tag>
);
}
}
export default AjaxUploader;