August 20, 2018

filereader is not working in angular 2 angular 4

filereader is not working in angular 2 angular 4. I found that
Filereader needs fileReader.readAsDataURL(event.target.files[0]) to start reading the file.

onSelectFile(event) { // called each time file input changes
// this.uploader.onSelectFile1(event);
if (event.target.files && event.target.files[0]) {

const fileReader: FileReader = new FileReader();
console.log(event);
fileReader.onloadstart = (e:any)=>{console.log(e)};
fileReader.onload = (event: Event) => {
console.log(fileReader.result);
};
fileReader.onerror = (e:any)=>{console.log(e)};

fileReader.readAsDataURL(event.target.files[0]); //reading the file

}
}

 

You may also like...