Skip to content

Instantly share code, notes, and snippets.

@weishai
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weishai/05b99dff6f2d9840e821 to your computer and use it in GitHub Desktop.
Save weishai/05b99dff6f2d9840e821 to your computer and use it in GitHub Desktop.
Image convert to Base64
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<input type='file' id="asd" />
<img id="img" src="" />
<div id="base"></div>
</body>
</html>
function readImage(input) {
if ( input.files && input.files[0] ) {
var FR= new FileReader();
FR.onload = function(e) {
$('#img').attr( "src", e.target.result );
$('#base').text( e.target.result );
};
FR.readAsDataURL( input.files[0] );
}
}
$("#asd").change(function(){
readImage( this );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment