Forked from resistancecanyon/multipleImageContentReader.js
Created
April 15, 2024 04:07
-
-
Save razvanioan/cae610b32e14d7001180795751888255 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