this is my first question on stackoverflow so please bear with my noobness. I am writing a cordova app with ionic framework and i am trying to upload multiple files from the camera to a server while displaying the uploaded files in a list and showing the progress for each of them. But i cant seem to figure out how to delete the files from the display list after a successfull upload. I am trying to use the ID of the File i get after i push it to the array, but those seem to be incorrect sometimes. E.g. the ID in the progress event is different than in the successevent and therefore i am removing the wrong file from the list or sometimes no file.
$scope.choosePic = function() {
window.imagePicker.getPictures(
function(results) {
for (var i = 0; i < results.length; i++) {
console.log('Image URI: ' + results[i]);
onSuccess(results[i]);
}
}, function (error) {
console.log('Error: ' + error);
}
);
}
$scope.delete = function ( item ) {
$scope.items.splice($scope.items.indexOf(item), 1);
}
var onSuccess = function(FILE_URI) {
console.log(FILE_URI);
$scope.picData = FILE_URI;
var filename = $scope.makeid();
var length = $scope.items.push({name: filename, image: $scope.picData, progress:0 });
$scope.$apply();
send($scope.picData,filename,length);
};
var onFail = function(e) {
console.log("On fail " + e);
}
send = function(imagedata,filename,length) {
var options = new FileUploadOptions();
var url = window.localStorage['URL'];
options.fileKey="file";
options.chunkedMode = false;
options.mimeType="image/jpeg";
options.headers = {Connection: "close"};
var params = {};
params.APIKEY = window.localStorage['APIKEY'];
params.DEVICEID = device.uuid;
options.params = params;
var ft = new FileTransfer();
var ID = length -1;
ft.onprogress = function(progressEvent) {
if (progressEvent.lengthComputable) {
var perc = Math.floor(progressEvent.loaded / progressEvent.total * 100);
if(!(typeof $scope.items[ID] == 'undefined')){
$scope.items[ID].progress=perc;
$scope.$apply();
console.log($scope.items[ID].name + "Upload progress: " + perc);
}
} else {
}
};
function onUploadSuccess(r) {
//alert("ID = " + ID + " Code = " + r.responseCode + "Response = " + r.response + "Sent = " + r.bytesSent);
//alert($scope.items[ID].name);
console.log("ID = " + ID + " Code = " + r.responseCode + "Response = " + r.response + "Sent = " + r.bytesSent);
$scope.items.splice(ID, 1); // remove uploaded image from list
$scope.$apply();
}
function onUploadFail(error) {
alert("An error has occurred: Code = " + error.code);
//console.log("upload error source " + error.source);
// console.log("upload error target " + error.target);
}
options.fileName=filename+".jpg";
ft.upload(imagedata, encodeURI(url), onUploadSuccess, onUploadFail, options);
}
});
I know this code probably rather clumsy but this the first time i am working with angular.js and cordova and the likes of it. Thanks for your help in advance