I'm trying to do multiple file upload using angular 2.
This is the function I'm using to extract data to variable
fileJSON
, which later I push into array fileArray: Array = [];.
var that2 = this.fileArray;
for (var i = 0; i < this.files.length; i++) {
(function (file) {
var reader = new FileReader();
let parts = file.name.split(".");
reader.onload = function (e) {
let view = new Uint8Array(this.result);
var data = Array.prototype.map.call(view, function (byte: any) {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('');
let fileJSON = { "Filename": parts[0], "Extension": parts[1], "DateCreated": new Date(), "Data": data, "TripId": "", "TypeId": 0 };
that2.push(fileJSON);
}
reader.readAsArrayBuffer(file);
})(this.files[i]);
}
this.editUploadedFiles();
After this I want to show a list of files by using
<tr ng-repeat="file in fileArray">
<td>{{file.Filename}}</td>
However this gives me
TypeError: Cannot read property 'Filename' of undefined
My editUploadedFiles() function where I want to get a list of all the files look like this:
editUploadedFiles() {
console.log(this.fileArray);
this.fileArray.forEach(function (element) {
console.log(element.Filename);
});
this.next(); // this just hides and shows some form div's and enables button that calls function "finish()"
}
However I get the same error while trying to print out element.Filename.
But when I press "Next" button again, I'm calling this function caled "finish()"
finish() {
this.fileArray.forEach(function (element) {
console.log(element.Filename);
});
}
Basically its almost the same but here it shows filenames without any problems!
I think it has to do something with asynchronous operations, but I have no how to await them.
this.fileArray.forEach(function (element) { console.log(element.Filename); });tothis.fileArray.forEach(function () { console.log(this.Filename); });this.fileArray.forEach(function (element) { console.log(element.Filename); });tofor (let item of this.fileArray) { console.log(item.Filename) }