Skip to content

Instantly share code, notes, and snippets.

@volkovasystems
Created October 30, 2014 03:07
Show Gist options
  • Save volkovasystems/4fcf92123304e727eed0 to your computer and use it in GitHub Desktop.
Save volkovasystems/4fcf92123304e727eed0 to your computer and use it in GitHub Desktop.
Convert loaded image to base64 encoding and display it in img element.
<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