1// Check for the File API support.
2if (window.File && window.FileReader && window.FileList && window.Blob) {
3 document.getElementById('files').addEventListener('change', handleFileSelect, false);
4} else {
5 alert('The File APIs are not fully supported in this browser.');
6}
7
8function handleFileSelect(evt) {
9 var f = evt.target.files[0]; // FileList object
10 var reader = new FileReader();
11 // Closure to capture the file information.
12 reader.onload = (function(theFile) {
13 return function(e) {
14 var binaryData = e.target.result;
15 //Converting Binary Data to base 64
16 var base64String = window.btoa(binaryData);
17 //showing file converted to base64
18 document.getElementById('base64').value = base64String;
19 alert('File converted to base64 successfuly!\nCheck in Textarea');
20 };
21 })(f);
22 // Read in the image file as a data URL.
23 reader.readAsBinaryString(f);
24}