-1

I am uploading image using php.

However, i want to see image preview of selected image before upload.

This is My form:

<form  enctype="multipart/form-data" id="fupForm" method="post">
  <input type="file" class="form-control" id="file" name="file" />
  <img class="basic-img" src="#"/>
</form>

I check the similar question Preview an image before it is uploaded But solution is not working for me

  • 1
    And what exactly does not work? Please describe it in details, and provide any screenshots if necessary. – sɐunıɔןɐqɐp Jun 12 '18 at 07:40
  • Have you got the JavaScript? – Liam G Jun 12 '18 at 07:41
  • This is my js code function readURL(input) { if (input.files && input.files[0]) { var reader = new FileReader(); reader.onload = function(e) { $('.basic-img').attr('src', e.target.result); } reader.readAsDataURL(input.files[0]); } } $("#file").change(function() { readURL(this); }); – Anuj Kumar Jun 12 '18 at 07:47

1 Answers1

0

Your Code which you mention in comment seems ok , if this is not working you can try it by calling function in onload

$(function() {
    $(":file").change(function() {
        if (this.files && this.files[0]) {
            var reader = new FileReader();
            reader.onload = imageIsLoaded;
            reader.readAsDataURL(this.files[0]);
        }
    });
});
function imageIsLoaded(e) {
    $('.basic-img').attr('src', e.target.result);
};
Bhavesh Taneja
  • 296
  • 1
  • 6