Created
December 28, 2016 02:30
-
-
Save herpiko/0f2eef6b0cc4a1a475b72521a10778fe to your computer and use it in GitHub Desktop.
simple file upload to base64 string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div> | |
<label for="filePicker">Choose or drag a file:</label><br> | |
<input type="file" id="filePicker"> | |
</div> | |
<br> | |
<div> | |
<h1>Base64</h1> | |
<textarea id="base64str" cols="50" rows="15"></textarea> | |
</div> | |
<script> | |
var fileHandler = function(evt) { | |
var files = evt.target.files; | |
var file = files[0]; | |
if (files && file) { | |
var reader = new FileReader(); | |
reader.onload = function(readerEvt) { | |
var binaryString = readerEvt.target.result; | |
document.getElementById("base64str").value = btoa(binaryString); | |
}; | |
reader.readAsBinaryString(file); | |
} | |
}; | |
document.getElementById('filePicker').addEventListener('change', fileHandler, false); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment