Skip to main content
Being Idea Innovations
How to select and display image using FileReader
Back to Blog

How to select and display image using FileReader

25 April 20181 min readJavascript
Share:
You have duplicate ids all over (imgInp and blah), so the selector will select only the first one, eventually your event is bound only to the first input field and the selection of $(‘#blah’) as well. You need to select the relative image with respect to the input. You dont have to duplicate the script as well. Select the next image tag relative to the input like $(input).next(‘img’).attr(‘src’, e.target.result);
function readURL(input) {

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

reader.onload = function (e) {
$('#blah').attr('src', e.target.result);
$("#blah").fadeIn(400).html('<img src="img/ajax-loader.gif" />');
}
reader.readAsDataURL(input.files[0]);
}
}

$(document).ready(function(){
$("#photoimg").change(function(){
readURL(this);
})

});

Using html to uploade and display image.

<img  id="blah" alt="" src="images/avatar.jpg">
<input type="file" name="photoimg" id="image" value="" />?
Share:

Want to talk tech?

We ship software that scales. Let's work together.

No long-term contracts
Senior engineers only
US · AU · NZ timezone coverage
14-day trial on retainers