forked from carsonmcdonald/direct-browser-s3-upload-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
121 lines (106 loc) · 2.53 KB
/
app.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
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
function createCORSRequest(method, url)
{
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr)
{
xhr.open(method, url, true);
}
else if (typeof XDomainRequest != "undefined")
{
xhr = new XDomainRequest();
xhr.open(method, url);
}
else
{
xhr = null;
}
return xhr;
}
function handleFileSelect(evt)
{
setProgress(0, 'Upload started.');
var files = evt.target.files;
var output = [];
for (var i = 0, f; f = files[i]; i++)
{
uploadFile(f);
}
}
/**
* Execute the given callback with the signed response.
*/
function executeOnSignedUrl(file, callback)
{
var xhr = new XMLHttpRequest();
xhr.open('GET', 'signput.php?name=' + file.name + '&type=' + file.type, true);
// Hack to pass bytes through unprocessed.
xhr.overrideMimeType('text/plain; charset=x-user-defined');
xhr.onreadystatechange = function(e)
{
if (this.readyState == 4 && this.status == 200)
{
callback(decodeURIComponent(this.responseText));
}
else if(this.readyState == 4 && this.status != 200)
{
setProgress(0, 'Could not contact signing script. Status = ' + this.status);
}
};
xhr.send();
}
function uploadFile(file)
{
executeOnSignedUrl(file, function(signedURL)
{
uploadToS3(file, signedURL);
});
}
/**
* Use a CORS call to upload the given file to S3. Assumes the url
* parameter has been signed and is accessible for upload.
*/
function uploadToS3(file, url)
{
var xhr = createCORSRequest('PUT', url);
if (!xhr)
{
setProgress(0, 'CORS not supported');
}
else
{
xhr.onload = function()
{
if(xhr.status == 200)
{
setProgress(100, 'Upload completed.');
}
else
{
setProgress(0, 'Upload error: ' + xhr.status);
}
};
xhr.onerror = function()
{
setProgress(0, 'XHR error.');
};
xhr.upload.onprogress = function(e)
{
if (e.lengthComputable)
{
var percentLoaded = Math.round((e.loaded / e.total) * 100);
setProgress(percentLoaded, percentLoaded == 100 ? 'Finalizing.' : 'Uploading.');
}
};
xhr.setRequestHeader('Content-Type', file.type);
xhr.setRequestHeader('x-amz-acl', 'public-read');
xhr.send(file);
}
}
function setProgress(percent, statusLabel)
{
var progress = document.querySelector('.percent');
progress.style.width = percent + '%';
progress.textContent = percent + '%';
document.getElementById('progress_bar').className = 'loading';
document.getElementById('status').innerText = statusLabel;
}