The solution provided in the approved response was very helpful to me.
ng2-file-upload offers an uploader
with a callback function this.uploader.onAfterAddingFile
. I simply invoked my change function and proceeded to read the file like so:
onFileChange(file: any) {
/* setting up file reader */
//const target: DataTransfer = <DataTransfer>(evt.target);
//if (target.files.length !== 1) throw new Error('Cannot use multiple files');
const reader: FileReader = new FileReader();
reader.onload = (e: any) => {
/* reading workbook */
const bstr: string = e.target.result;
const wb: XLSX.WorkBook = XLSX.read(bstr, { type: 'binary' });
/* extracting first sheet */
const wsname: string = wb.SheetNames[0];
const ws: XLSX.WorkSheet = wb.Sheets[wsname];
/* saving data */
var data = <any>(XLSX.utils.sheet_to_json(ws, { header: 1 }));
this.totalInventories = data.length > 0 ? data.length - 1 : 0;
};
if (file._file)
reader.readAsBinaryString(file._file);
}
I hope this explanation proves beneficial to others as well :)