Reputation: 35
using the practical example in the ionic framework documentation, my implementation worked perfectly, but my problem now is to iterate through the array of images, so that I can send selected multiple images to the server, I tried multiple methods, the for-loop, and the do-while and normal
SelectCarImages(){
this.options = {
width: 600,
height: 400,
quality: 75,
outputType: 1
};
this.imagePicker.getPictures(this.options).then((results) => {
for (var i = 0; i < results.length; i++) {
this.photos.push('data:image/jpeg;base64,' + results[i]);
this.pictureData = results[i];
//PictureData is the array of images
}
}, (err) => {
(err);
});
console.log('choose photo');
}
}
uploadMultipleImages(){
var imageArr;
for (var i = 0; i < this.pictureData.length; i++) {
imageArr = this.pictureData[i]
}
let body = {
name: "pix.jpg",
foto_type: "Cars",
file: imageArr
};
this.routes.postData("/fotos", body).subscribe(
data => {
let response = data as any;
let ret = JSON.parse(response._body);
if (ret) {
console.log("pictures uploaded succesfully");
} else {
console.log(ret);
}
},
error => {
console.log(error);
}
);
}
Upvotes: 0
Views: 506
Reputation: 21
As you have mentioned that you like to best appropriate way to upload the image. Don't use base64 as it will be a long string and it will take more CPU of your mobile devices. Instead of you can use File_URI and on a backend side you can create the image from that FILE_URI using POST or FILES in PHP. If you create an object array for those image files. You can loop through them on the backend level and it will be much-optimized way than base64.
Upvotes: 1