Skip to content

Instantly share code, notes, and snippets.

@herpiko
Created December 28, 2016 02:30
Show Gist options
  • Save herpiko/0f2eef6b0cc4a1a475b72521a10778fe to your computer and use it in GitHub Desktop.
Save herpiko/0f2eef6b0cc4a1a475b72521a10778fe to your computer and use it in GitHub Desktop.
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