Skip to content

Instantly share code, notes, and snippets.

@ushios
Created March 30, 2017 04:56
Show Gist options
  • Save ushios/51dd516b7ba4b43eb2a483101bed639c to your computer and use it in GitHub Desktop.
Save ushios/51dd516b7ba4b43eb2a483101bed639c to your computer and use it in GitHub Desktop.
Base64 encoding using javascript on form file
<html>
<head>
<title>base64test</title>
</head>
<form>
<input id="file" type="file" />
</form>
<input id="button" type="button" value="encode"/>
<br />
<textarea id="encoded" style="width:1000px; height:800px;"></textarea>
<script type="text/javascript">
document.getElementById('button').onclick=function(){
console.log("button clicked.");
var form = document.querySelector("#file").files[0];
getBase64(form);
}
function getBase64(file) {
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function () {
document.querySelector('#encoded').value = reader.result;
};
reader.onerror = function (error) {
console.log('Error: ', error);
};
}
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment