Created
October 30, 2014 03:07
-
-
Save volkovasystems/4fcf92123304e727eed0 to your computer and use it in GitHub Desktop.
Convert loaded image to base64 encoding and display it in img element.
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> | |
<body> | |
<p>Select a File to Load:</p> | |
<input id="inputFileToLoad" type="file" onchange="loadImageFileAsURL();" /> | |
<br /> | |
<p>File Contents as DataURL:</p> | |
<textarea id="textAreaFileContents" style="width:640;height:240" ></textarea> | |
<img id="testImage" /> | |
<script type='text/javascript'> | |
function loadImageFileAsURL( ){ | |
var filesSelected = document.getElementById( "inputFileToLoad" ).files; | |
if( filesSelected.length > 0 ){ | |
var fileToLoad = filesSelected[ 0 ]; | |
var fileReader = new FileReader( ); | |
fileReader.onload = function( fileLoadedEvent ){ | |
var textAreaFileContents = document.getElementById( "textAreaFileContents" ); | |
textAreaFileContents.innerHTML = fileLoadedEvent.target.result; | |
var testImage = document.getElementById( "testImage" ); | |
testImage.setAttribute( "src", fileLoadedEvent.target.result ); | |
}; | |
fileReader.readAsDataURL( fileToLoad ); | |
} | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment