Skip to content

Instantly share code, notes, and snippets.

@nhodges
Created July 14, 2017 05:20
Show Gist options
  • Save nhodges/dc9403645a6bf9b525a2f5e24d5f2a77 to your computer and use it in GitHub Desktop.
Save nhodges/dc9403645a6bf9b525a2f5e24d5f2a77 to your computer and use it in GitHub Desktop.
Read file input (image) as data URL
<form id="form1" runat="server">
<input type='file' id="imgInp" />
<img id="blah" src="#" alt="your image" />
</form>
<script type="text/javascript">
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#blah').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#imgInp").change(function(){
readURL(this);
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment