forked from harshavardhana/minio-js-browser-upload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index-post.html
48 lines (39 loc) · 1.16 KB
/
index-post.html
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
<input type="file" id="selector" multiple>
<button onclick="upload()">Upload</button>
<div id="status">No uploads</div>
<script src="//code.jquery.com/jquery-3.1.0.min.js"></script>
<script type="text/javascript">
function upload() {
[$('#selector')[0].files].forEach(fileObj => {
var file = fileObj[0]
// Retrieve a URL from our server.
retrieveNewURL(file, obj => {
// Upload the file to the server.
uploadFile(file, obj)
})
})
}
// Request to our Node.js server for an upload URL.
function retrieveNewURL(file, cb) {
$.get(`/presignedUrl?name=${file.name}`, (obj) => {
cb(obj)
})
}
// Use XMLHttpRequest to upload the file to S3.
function uploadFile(file, obj) {
var xhr = new XMLHttpRequest ()
var objSon = JSON.parse(obj);
xhr.open('POST', objSon.urlStr.postURL, true);
var fd = new FormData();
Object.entries(objSon.urlStr.formData).forEach(([key, value]) => {
fd.append(key, value);
});
fd.append('file', file);
xhr.send(fd);
xhr.onload = () => {
if (xhr.status == 204) {
$('#status').text(`Uploaded ${file.name}.`)
}
}
}
</script>