Skip to content

Instantly share code, notes, and snippets.

@akhileshdarjee
Created April 22, 2019 12:56
Show Gist options
  • Save akhileshdarjee/a7ad4a7edbf394a63ffe5e428188fdb2 to your computer and use it in GitHub Desktop.
Save akhileshdarjee/a7ad4a7edbf394a63ffe5e428188fdb2 to your computer and use it in GitHub Desktop.
Render image file in HTML5
<!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