Created
April 22, 2019 12:56
-
-
Save akhileshdarjee/a7ad4a7edbf394a63ffe5e428188fdb2 to your computer and use it in GitHub Desktop.
Render image file in HTML5
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
<!DOCTYPE html> | |
<html lang="en"> | |
<body> | |
<form name="user-form" id="user-form" enctype="multipart/form-data"> | |
<input type="file" name="image_file"> | |
<input type="text" name="name"> | |
<img src="" id="image_file"> | |
<button type="button" title="Save" id="save-user">Save</button> | |
</form> | |
<script type="text/javascript"> | |
$("form").on("change", "input[type='file']", function() { | |
if ($(this).val()) { | |
read_image(this); | |
} | |
}); | |
// render image files | |
function read_image(input) { | |
if (input.files && input.files[0]) { | |
var reader = new FileReader(); | |
reader.onload = function (e) { | |
$(input).parent().find('img').attr('src', e.target.result); | |
} | |
reader.readAsDataURL(input.files[0]); | |
} | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment