2
Eae you guys, blz?
I need a code that when uploading an image via file type input it is displayed in a type of "preview".
What I got so far is this:
HTML:
<div class="card">
<img class="card-img-top" src="<?=$foto?>" id="fotopreview">
<div class="card-body">
<h4 class="card-title">Alterar foto do aluno</h4>
<p class="card-text">Selecione uma imagem no botão abaixo</p>
<input type="file" class="form-control-file" name="foto" id="uploadfoto">
</div>
</div>
JS:
uploadfoto = document.getElementById('uploadfoto');
fotopreview = document.getElementById('fotopreview');
uploadfoto.addEventListener('change', function(e) {
showThumbnail(this.files);
});
function showThumbnail(files) {
if (files && files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#fotopreview').attr('src', e.target.result);
}
reader.readAsDataURL(files[0]);
}
}
This code ai does not change the src attribute of the "fotopreview".
Someone can lend a hand?
Vlw!
what error appears to you? The code you posted is working, maybe you forgot to add the Jquery reference
– Leandro Angelo
Nothing appears, just doesn’t change the src attribute. I even look at the source code of the page after loading the image, and the src attribute still has nothing. Have you tested it? If tested and worked, which version of jquery ta using?
– Thiago Costa