Skip to content

Instantly share code, notes, and snippets.

@rintoug
Created August 23, 2018 05:59
Show Gist options
  • Save rintoug/e75973ade7e75596f474bf6d3a86f252 to your computer and use it in GitHub Desktop.
Save rintoug/e75973ade7e75596f474bf6d3a86f252 to your computer and use it in GitHub Desktop.
Show preview when select an image via file input
<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