-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathuploader
48 lines (41 loc) · 1.08 KB
/
uploader
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
function postData(
url,
data = {},
{type = 'json', fileKey = 'file', headers = [], onProgress} = {}
) {
const req = new XMLHttpRequest();
const formData = new FormData();
req.open('POST', url, true);
if(data instanceof File) {
formData.append(fileKey, data, data.name);
data = formData;
} else if(type === 'multipart') {
for(const key in data) {
formData.append(key, data[key]);
}
data = formData;
} else {
data = JSON.stringify(data);
req.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
}
for(const key in headers) {
req.setRequestHeader(key, headers[key]);
}
return new Promise((res, rej) => {
req.onload = (e) => {
if (req.status === 200) {
res(e);
} else {
rej(e);
}
}
if(typeof onProgress === 'function') {
req.upload.onprogress = onProgress;
}
req.onerror = rej;
req.onabort = rej;
req.onabort = rej;
req.ontimeout = rej;
req.send(data);
});
}