Skip to content

Instantly share code, notes, and snippets.

@zubayerahamed
Last active February 5, 2018 17:26
Show Gist options
  • Save zubayerahamed/15220877ac705c9009f3feb6c2881ed9 to your computer and use it in GitHub Desktop.
Save zubayerahamed/15220877ac705c9009f3feb6c2881ed9 to your computer and use it in GitHub Desktop.
View image preview in a div when uploading
<!DOCTYPE html>
<html>
<head>
<title>Image Preview</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<style type="text/css">
#uploadPreview {
border: 1px solid red;
width: 200px;
height: 200px;
overflow: hidden;
}
#uploadPreview img {
min-width: 100%;
min-height: 100%;
}
</style>
</head>
<body>
<input type="file" id="file"/>
<div id="uploadPreview"></div>
<script type="text/javascript">
var _URL = window.URL || window.webkitURL;
$("#file").change(function(e) {
var image, file;
if ((file = this.files[0])) {
image = new Image();
image.onload = function() {
src = this.src;
$('#uploadPreview').html('<img src="'+ src +'"></div>');
e.preventDefault();
}
};
image.src = _URL.createObjectURL(file);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment