Created
October 9, 2017 08:49
-
-
Save pankajpatel/d338d703c7de83262f7859e642f85e2d to your computer and use it in GitHub Desktop.
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
const generatePreviewData = (file, callback) => { | |
const fr = new FileReader(); | |
fr.addEventListener('load', (e) => { | |
if(callback && typeof callback === 'function') callback(fr.result); | |
}); | |
fr.readAsDataURL(file); | |
} |
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
const generatePreviewData = (file) => { | |
const fr = new FileReader(); | |
return new Promise((resolve, reject) => { | |
fr.addEventListener('load', (e) => { | |
resolve(fr.result); | |
}); | |
fr.addEventListener('error', (e) => { | |
reject(); | |
}); | |
fr.readAsDataURL(file); | |
}); | |
} |
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
// Import the generatePreviewData function as following ways: | |
// const generatePreviewData = require('./preview-file'); | |
// or <script src="./preview-file.js"></script> | |
// OR copy and paste above function in the same file of usage | |
const photo = document.querySelector('#photo'); | |
generatePreviewData(photo.files[0], (data) => { | |
const img = document.createElement('img'); | |
img.src = data; | |
img.height = 200; | |
document.body.appendChild(img) | |
}) |
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
// Import the generatePreviewData function as following ways: | |
// const generatePreviewData = require('./preview-file'); | |
// or <script src="./preview-file.js"></script> | |
// OR copy and paste above function in the same file of usage | |
const photo = document.querySelector('#photo'); | |
generatePreviewData(photo.files[0]) | |
.then((data) => { | |
const img = document.createElement('img'); | |
img.src = data; | |
img.height = 200; | |
document.body.appendChild(img) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment