In my Angular2 application, I have a file upload feature that sends files as byte arrays to a web service. To create the byte array, I am using a FileReader with an onload event. However, I am encountering an issue where I cannot reference my uploadService within this event using this.uploadService
.
public uploadHandler(event) {
var reader = new FileReader();
reader.onload = function() {
var arrayBuffer = this.result, array = new Uint8Array(arrayBuffer), binaryString = String.fromCharCode.apply(null, array);
//this.uploadService.DoUpload(binaryString);
}
reader.readAsArrayBuffer(event.files[0]);
}
I attempted to use the fat arrow function, but then I could no longer reference the result variable.
reader.onload = () => {
//this.result does not exist anymore!
var arrayBuffer = result, array = new Uint8Array(arrayBuffer), binaryString = String.fromCharCode.apply(null, array);
this.uploadService.DoUpload(binaryString);
}