1
I’ve seen some similar topics, followed the recommendations and was unsuccessful, so I open up a new topic. This error occurs when I upload an image and at the time of displaying it this message appears. PS: I already checked the page imports and everything is ok.
HTML
<div class="form-group col-md-12" >
<div class="form-group">
<input type="file" id="file" onchange="visualizarImg();" />
</div>
</div>
<div class=" form-group col-md-6">
<img id="imagemPromocao" src="" class="img-responsive" />
</div>
</div>
Javascript
window.onload = function() {
function visualizarImg() {
var preview = document.querySelectorAll('img').item(1);
var file = document.querySelector('input[type=file]').files[0];
var reader = new FileReader();
reader.onloadend = function() {
preview.src = reader.result;// carrega em base64 a img
};
if (file) {
reader.readAsDataURL(file);
} else {
preview.src = "";
}
}
}
If I put off the onload I will have the following error :
Cannot set property 'src' of null at FileReader.reader.onloadend
– Eduardo Krakhecke
Then leave the function inside the onload and add the event also inside the onload.
– user83428