I'm working on Angular4 project. I get Blob returned from my API call, then I convert it to base64 but can't manage to save it to the array of pictures (so I can show it with *ngFor
later).
Here is my API call:
getImg(): Observable<Blob> {
const path = *can't show this part*;
return this.http.get(path, { responseType: "blob" });
}
And here is what I tried so far:
This function has error at line this.images[i] = reader.result;
, because it says Property 'images' does not exist on type 'FileReader'
images: Array<any> = [];
getImages(): void {
for (var i = 0; i < this.myData.length; i++) {
this.myApiCalls.getImg()
.subscribe(res => {
var reader = new FileReader();
reader.readAsDataURL(res);
reader.addEventListener("loadend", function () {
this.images[i] = reader.result;
});
},
err => {
console.log(err.message)
});
}
}
The other thing I tried is with callbacks, but I still got error, on the same column but for different thing. It says 'this' implicitly has type 'any' because it does not have a type annotation.
getImages(): void {
for (var i = 0; i < this.myData.length; i++) {
this.myApiCalls.getImg()
.subscribe(res => {
this.readImageFile(res, function(e: any) {
this.fields[i] = e.target.result;
});
},
err => {
console.log(err.message)
});
}
}
readImageFile(response: Blob, callback: any): void {
var reader = new FileReader();
reader.readAsDataURL(response);
reader.onloadend = callback
}
So I get data returned back correctly, but the problem is I can't manage to save it to the array. If you guys are able to help me solve that problem I would be very happy. Thank you.