Skip to content

Instantly share code, notes, and snippets.

@chrisgfortes
Created July 24, 2015 19:20
Show Gist options
  • Save chrisgfortes/b4e035274d4b599d0acb to your computer and use it in GitHub Desktop.
Save chrisgfortes/b4e035274d4b599d0acb to your computer and use it in GitHub Desktop.
Ler imagem e mostrar preview sem upload do arquivo, somente client
<img id="preview" src="placeholder.png" height="100px" width="100px" />
<input type="file" name="image" onchange="previewImage(this)" accept="image/*"/>
<script type="text/javascript">
function previewImage(input) {
var preview = document.getElementById('preview');
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
preview.setAttribute('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
} else {
preview.setAttribute('src', 'placeholder.png');
}
}
</script>
Referência: http://stackoverflow.com/questions/5256620/how-can-i-show-a-image-preview-in-the-browser-without-uploading-the-image-file-t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment