Change src attribute of an image when uploading via file type input

Asked

Viewed 1,061 times

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

  • 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?

1 answer

2


It seems to me that in your case the error may be happening due to Jquery missing, follows the excerpt of pure javascript (jquery was only being used in a single line)

var uploadfoto = document.getElementById('uploadfoto');
var 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.src = e.target.result;
    }

        reader.readAsDataURL(files[0]);
    }
}
<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>

  • Thanks for the expensive answer, but it’s not working yet :/ Do you think it would work by relating the "onchange" input directly to the function that changes the src attribute? If yes, how would I send the file by parameter? Thanks!

  • didn’t work on your page or here in reply snipet?

  • I hadn’t seen the kkkk snipet. It didn’t work on my page, what could it be? I even created a separate document only with these snippets and still nothing.

  • You are probably putting js before html, it will have to stay at the end to work.

  • That’s right, Marcelo. Thank you both, you saved me

Browser other questions tagged

You are not signed in. Login or sign up in order to post.