I'm trying to send to the template some image information after validation. But I'm not getting.
The variables that go to the template are within a callback, or are within an asynchronous function, and I do not know to send this information to the template.
Could help me solve this problem? I've tried everything.
event.component.ts
import "rxjs/Rx";
import {Component} from '@angular/core';
@Component({
selector: '<my-image></my-image>',
templateUrl: './event.component.html',
})
export class EventComponent {
files: File[];
file: any
imageName: any;
imageSize: any;
whithImage = false;
ngOnInit() {this.files = []}
onFileSelect(event) {
this.files = event.dataTransfer ? event.dataTransfer.files : event.target.files;
this.file = this.files[0];
this.isImage(this.file, function(res, file){
if(res==true){
this.imageName = file.name;
this.imageSize = file.size;
this.whithImage = res;
}
})
}
isImage(file, callback) {
var reader = new FileReader();
reader.onload = function(event) {
var contents = reader.result;
var buf = new Uint8Array(contents);
if(buf[0] === 0xFF && buf[1] === 0xD8 && buf[2] === 0xFF){
callback(true, file) //jpg
}else{callback(false)}
};
reader.readAsArrayBuffer(file);
}
onChooseClick(event, fileInput) {
fileInput.value = null;
}
event.component.html
<span (click)="onChooseClick($event, fileinput)">
Image<input #fileinput type="file" (change)="onFileSelect($event)"/>
</button>
<div *ngIf="whithImage">
<p>{{ imageName }}</p>
<p>{{ imageSize }}</p>
</div>