Skip to content

Instantly share code, notes, and snippets.

@JoeThunyathep
Created March 21, 2022 05:30
Show Gist options
  • Save JoeThunyathep/2142bfd9697747525f3b726557d74e05 to your computer and use it in GitHub Desktop.
Save JoeThunyathep/2142bfd9697747525f3b726557d74e05 to your computer and use it in GitHub Desktop.
<input type="file" id="fs" multiple> <br>
<small id="target"></small>
// Prepare a function to decode base 64 unicode
function b64DecodeUnicode(str) {
return decodeURIComponent(Array.prototype.map.call(atob(str), function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
}
// Create file reader Event Listener
const reader = new FileReader();
reader.addEventListener('load', (event) => {
eventdata = event.target;
const result_base_64 = event.target.result;
const result_decoded = b64DecodeUnicode(result_base_64.split(',')[1]);
console.log(result_decoded);
document.getElementById("target").innerHTML = result_decoded;
});
// Event Listener for file input
const fileSelect = document.getElementById('fs');
fileSelect.addEventListener('change', (event) => {
const fileList = event.target.files;
for (const file of fileList) {
console.log("read file");
reader.readAsDataURL(file);
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment