Forked from resistancecanyon/multipleImageContentReader.js
Created
February 20, 2022 11:11
-
-
Save minlaxz/6c0666d8c04dacfb142587d8d0a5d0fd to your computer and use it in GitHub Desktop.
Reading Multiple Image Base64 data using Es6 Async/Await in a html:type:file input
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
// html | |
// <input type="file" onchange="imagesSelected" multiple accept=".gif,.jpg,.jpeg,.png" /> | |
async function imagesSelected(event) { | |
let files = [...event.target.files]; | |
let images = await Promise.all(files.map(f=>{return readAsDataURL(f)})); | |
//all images' base64encoded data will be available as array in images | |
} | |
function readAsDataURL(file) { | |
return new Promise((resolve, reject)=>{ | |
let fileReader = new FileReader(); | |
fileReader.onload = function(){ | |
return resolve({data:fileReader.result, name:file.name, size: file.size, type: file.type}); | |
} | |
fileReader.readAsDataURL(file); | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment