I am trying to read a folder via an input and save the content of each of the individual files containt in the folder in an array.
It is all done with this Input:
<div class="form-group">
<input type="file" id="file" (change)="accessFiles($event)" webkitdirectory directory multiple >
</div>
And this FileReader:
files = []
readFolder(fileChangeEvent: Event) {
this.readFile(0, (fileChangeEvent.target as HTMLInputElement).files);
}
private readFile(index, files) {
const reader = new FileReader();
if (index >= files.length ) {
console.log(this.files);
return;
}
const file = files[index];
reader.onload = (e: any) => {
const bin = e.target.result;
this.files.push(e.target.result);
this.readFile(index + 1, files);
};
reader.readAsBinaryString(file);
}
Now, the console.log in readFile displays the array and it looks fine, the only thing now is that i want to use it later in the code, for that i am trying to return it.
I tried something like this:
accessFiles(data){
const contentList = this.readDocument(data));
}
which did not work. the output turned out to be undefined
What i also tried was subscribing to the data:
accessFiles(data){
this.readFolder(data).subscribe(values => {
}
}
but then i got this error:
Property 'subscribe' does not exist on type 'void'.
Which is why I tried to implement an Observable:
files = []
readFolder(fileChangeEvent: Event) {
return this.readFile(0, (fileChangeEvent.target as HTMLInputElement).files);
}
private readFile(index, files) {
return new Observable<any>(obs => {
const reader = new FileReader();
if (index >= files.length ) {
console.log(this.files);
obs.next({result: this.files});
return;
}
const file = files[index];
reader.onload = (e: any) => {
const bin = e.target.result;
this.files.push(e.target.result);
this.readFile(index + 1, files);
};
reader.readAsBinaryString(file);
});
}
But the only Output I got out of this was:
Observable {_isScalar: false, _subscribe: ƒ}
Which I am not really sure what that means...
How can i correctly access a List from the FileReader? or what am I doing wrong?