Created
July 20, 2020 08:44
-
-
Save heytulsiprasad/14abe21ea4c1f76e8123229b0984df68 to your computer and use it in GitHub Desktop.
How to display a selected image from user environment?
This file contains hidden or 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
| // file: selected/dropped by user | |
| function displayImage(file) { | |
| const reader = new FileReader() | |
| reader.readAsDataURL(file) // reader.result will be `data:__` | |
| reader.onload = (e) => imageRef.current.src = reader.result | |
| console.log(reader.result) // returns `data:__` which is URL of files data | |
| } | |
| // reader.readAsBinaryString: The result is a JavaScript ArrayBuffer containing binary data | |
| // reader.readAsDataURL: The result is a string with a data: URL representing the file's data | |
| // reader.readAsBinaryString: The result contains the raw binary data from the file in a string | |
| // reader.readAsText: The result is text in a string |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment