-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.js
More file actions
35 lines (30 loc) · 852 Bytes
/
upload.js
File metadata and controls
35 lines (30 loc) · 852 Bytes
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
'use strict'
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest()
if ("withCredentials" in xhr) {
// XHR for Chrome/Firefox/Opera/Safari.
xhr.open(method, url, true)
} else if (typeof XDomainRequest !== "undefined") {
// XDomainRequest for IE.
xhr = new XDomainRequest()
xhr.open(method, url)
} else {
// CORS not supported.
xhr = null
}
return xhr
}
function ajaxUpload({url, name, cors, file, onProgress, onLoad, onError, withCredentials}) {
let data = new FormData()
data.append(name, file)
let xhr = createCORSRequest('post', url, cors)
xhr.withCredentials = withCredentials
xhr.upload.addEventListener('progress', onProgress, false)
xhr.onload = onLoad
xhr.onerror = onError
xhr.send(data)
return xhr
}
export default function (args) {
return ajaxUpload(args)
}