2

I have modified the function

<script>
function readURL(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();
        reader.onload = function(e) {
            $('#blah').attr('src', e.target.result);
        }

        reader.readAsDataURL(input.files[0]);
    }
}
$("#upload").change(function() {
    readURL(this);
});
</script>

and included this input in my form in the body element:

<input type="file" name="uploaded_files" id="upload"/>
<img id="blah" src="#" alt="your image" />

I have also included jQuery:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

It’s working in JsFiddle, but it’s not working on my localhost.

Palec
  • 10,298
  • 7
  • 52
  • 116
Kevin
  • 63
  • 4
  • 11

2 Answers2

2

There is absolutely nothing wrong with what you have done, neither with your script JS block. You can go and check on this JSFiddle.


Just make sure you are loading the jQuery library correctly since your script depends on.

tmarwen
  • 11,491
  • 4
  • 35
  • 54
1

Try Like

function readURL(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {
            $('#blah').attr('src', e.target.result);
        }

        reader.readAsDataURL(input.files[0]);
    }
}

$("#imgInp").change(function(){
    readURL(this);
});

Demo

cracker
  • 4,808
  • 3
  • 18
  • 37