Display image to be loaded PHP

Asked

Viewed 1,608 times

1

Hello I would like to know how to display an image that is to be loaded into a INPUT FILE

For example, when the user clicks to upload the image and after selecting it how do I view an image Preview before the form Ubmit?

  • Study how to load files using the File API. It is prerequisite to have Javascript knowledge to implement this type of functionality.

1 answer

2

This approach solves this problem in a simple way using only jquery:

<script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
<input id="imgInput" type="file">
<script>
$("#imgInput").change(function(){
    if (this.files && this.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {
            $('#view-img').attr('src', e.target.result);
        }
        reader.readAsDataURL(this.files[0]);
    }
});
</script>

<img id="view-img" src="default.jpg">
  • Can you explain the code a little? It will display the image by the file path on the user’s machine or does a parallel upload?

  • https://developer.mozilla.org/en-US/docs/Web/API/FileReader e http://caniuse.com/#feat=filereader

  • It does not upload, just shows the image at runtime, straight from the machine. Uploading is another story.

Browser other questions tagged

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