-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
51 lines (50 loc) · 1.57 KB
/
index.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
49
50
51
<!DOCTYPE html>
<html>
<head>
<title>
Shuff Shuff Shuffle
</title>
<meta charset="utf-8"/>
</head>
<body>
<div class="shuff">Let's shuffle.</div>
<form action="/service" method="post" accept-charset="utf-8">
<input type="file" name="file" id="file" value=""/>
<p>
<button class="submit" type="submit">Yes, lets.</button>
</p>
</form>
<script>
var img = document.createElement('img');
var payload = {content:{},meta:{}};
document.querySelector('.submit').addEventListener('click', function(e) {
e.preventDefault();
var xhr = new XMLHttpRequest();
xhr.onload = function() {
if(this.responseText) {
img.src = JSON.parse(this.responseText).content.data;
}
}
xhr.open('POST', '/service');
xhr.setRequestHeader('Content-Type', 'application/json');
if(payload.content.data) {
xhr.send(JSON.stringify(payload));
}
});
document.querySelector('input[type=file]').addEventListener('change', function(e) {
var reader = new FileReader();
reader.onloadend = function() {
document.querySelector('.shuff').appendChild(img);
img.src = reader.result;
payload.content.type = e.target.files[0].type;
payload.content.data = reader.result;
}
if (e.target.files[0].type.indexOf('image') !== 0) {
alert('Shut it down. Images plz');
} else {
reader.readAsDataURL(e.target.files[0]);
}
});
</script>
</body>
</html>