-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile-api-example.html
60 lines (50 loc) · 1.47 KB
/
file-api-example.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
52
53
54
55
56
57
58
59
60
<!DOCTYPE html>
<html>
<head>
<title>File API Example</title>
<meta name='viewport' content='width=device-width, initial-scale=1.0' />
<!-- <script src='./resources/app.js'></script>
<link rel='stylesheet' type='text/css' href='./resources/style.css'> -->
<style type='text/css'>
</style>
<h1>
File API Example
</h1>
<input id='upload' type='file' accept='audio/*;capture=microphone'>
</head>
<body>
</body>
<script>
// Check for the various File API support.
if (window.File && window.FileReader && window.FileList && window.Blob) {
// Great success! All the File APIs are supported.
} else {
alert('The File APIs are not fully supported in this browser.');
}
/** Demonstrates the functional steps needed to process a file into a binary string */
var processUpload = function (event) {
//keyed object containing all of the files uploaded
var file = event.target.files[0];
var bytes;
var base64;
console.log(file.name);
// Filter out images of a certain type
if (!file.type.match('image.*')) {
return;
}
var reader = new FileReader();
//pass in the callbacks here
reader.onload = console.log('reader starting');
reader.onloadend = function(evt) {
if (evt.target.readyState == FileReader.DONE) { // DONE == 2
bytes = reader.result;
base64 = btoa(reader.result);
console.log(bytes)
console.log(base64)
}
}
reader.readAsBinaryString(file);
}
document.getElementById('upload').addEventListener('change', processUpload, false);
</script>
</html>