Created
August 23, 2018 05:59
-
-
Save rintoug/e75973ade7e75596f474bf6d3a86f252 to your computer and use it in GitHub Desktop.
Show preview when select an image via file input
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
<html lang="en"> | |
<head> | |
<title>Show preview when select an image via file input </title> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> | |
</head> | |
<body> | |
<input type="file" name="image_file" id="image_file"> | |
<br> | |
<img src="" id="image_file_tag" width="300px" /> | |
<script type="text/javascript"> | |
function readIMGURL(input) { | |
if (input.files && input.files[0]) { | |
var reader = new FileReader(); | |
reader.onload = function (e) { | |
$('#image_file_tag').attr('src', e.target.result); | |
} | |
reader.readAsDataURL(input.files[0]); | |
} | |
} | |
$("#image_file").change(function(){ | |
readIMGURL(this); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment