Reading data from a file input in Angular

 A simple example:

<input type="file" (change)="onFileSelected($event)"/>

Then in the code itself.

onFileSelected($event) {
    if ($event.target.files.length == 0)
        return 0;
    
    let file = $event.target.files[0];
    
    let fileReader = new FileReader();
    fileReader.onload = () => {
        console.log(fileReader.result);
    }
    fileReader.readAsDataURL(file);

}

Post a Comment

Previous Post Next Post