I’m new to typescript and Ionic 2, I’m trying to create a clean code, to take multiple images and convert to base64, so I’ll need to use “Promises”, my final goal is to get this list of base64 images and send them to my server. As I am working with heavy code I would like to make it very clean and that works, I would like to know everyone’s opinion, Thank you. My function to get the pictures is the getImgs().
However Promise.All never called.
I can not figure out what I’m missing, everything seems right.
import { Component } from '@angular/core';
import { NavController, NavParams, ModalController } from 'ionic-angular';
import { ImagePicker } from 'ionic-native';
@Component({
selector: 'page-cadastrar-post',
templateUrl: 'cadastrar-post.html'
})
export class CadastrarPostPage {
imgs: Array<any> =[];
constructor(public navCtrl: NavController, public navParams: NavParams, public modalCtrl: ModalController) {
}
ionViewDidLoad() {
console.log('ionViewDidLoad CadastrarPostPage');
}
convertFileToDataURLviaFileReader(url) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.onload = function () {
var reader = new FileReader();
reader.onloadend = function () {
resolve(reader.result);
// callback(reader.result);
}
reader.readAsDataURL(xhr.response);
};
xhr.open('GET', url);
xhr.responseType = 'blob';
xhr.send();
});
}
getImgs() {
var options = {
maximumImagesCount: 5,
quality: 80
};
ImagePicker.getPictures(options).then((results) => {
for (var i = 0; i < results.length; i++) {
this.imgs.push(this.convertFileToDataURLviaFileReader(results[i]));
}
}, (err) => { });
Promise.all(this.imgs).then((results: any[]) => {
alert(results[0]);
//finally
});
}
}