Skip to content

Instantly share code, notes, and snippets.

@herpiko
Created Dec 28, 2016
Embed
What would you like to do?
simple file upload to base64 string
<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