I deleted the question Variables and Scope TypeScript because as a solution I did the validations I needed within the function of the onload
itself event I think the solution changed the question structure, but I'm having a problem, the function always returns true
even when it enters the validations that should return false
:
isFileValid( file : File ) : boolean {
let image = new Image();
image.src = URL.createObjectURL( file );
image.onload = () : boolean => {
let imageH = image.height;
let imageW = image.width;
if ( imageH != this.maxheight || imageW != this.maxwidth ){
let notifications: Message = {
id: Math.random() * 10,
type: 'alert-danger',
button: true,
content: 'a imagem ' + file.name
+ " precisa ter exatamente as dimensões "
+ '<strong>${this.maxwidth}</strong>x<strong>${this.maxheight}px!'
};
this.notificationsService.add(notifications);
return false;
}
if ( file.size > ( this.maxsize * this.MB ) ) {
let notifications: Message = {
id: Math.random() * 10,
type: 'alert-danger',
button: true,
content: 'a imagem' + file.name
+ 'ultrapassa o limite de ${ this.FormateBytes( this.maxsize * this.MB )}'
};
this.notificationsService.add(notifications);
return false;
}
return true;
}
return true;
}
How can I do to call if (isFileValid(imagem) )
to return true
or false
?