0👍
✅
You have scope problem. You lost reference for this
, as you are in nested function, which have own this
object. Improve your code:
eadfile(id) {
let f = document.getElementById(id).files[0];
console.log(f);
let r = new FileReader();
r.readAsArrayBuffer(f);
var self = this // ADD THIS
r.onload = function (e) {
console.log(e);
let data = r.result;
console.log('data: ' + data);
let bytes = new Uint8Array(data);
console.log('bytes: ' + bytes);
let b64encoded = self.bufferToBase641(bytes); // CHANGE THIS
console.log(b64encoded);
};
},
You can also use the arrow function for you r.onload
callback:
eadfile(id) {
let f = document.getElementById(id).files[0];
console.log(f);
let r = new FileReader();
r.readAsArrayBuffer(f);
r.onload = e => { // CHANGE JUST THIS LINE
console.log(e);
let data = r.result;
console.log('data: ' + data);
let bytes = new Uint8Array(data);
console.log('bytes: ' + bytes);
let b64encoded = this.bufferToBase641(bytes);
console.log(b64encoded);
};
},
Source:stackexchange.com