I am programming an application with angular and ionic compatible with android, I need to upload 5 images to a server and use $ cordovaFileTransfer for this purpose, the problem is that trying to upload the 5 images what it does is upload 5 times the last photo, any idea of this error ?, I read somewhere that is a bug of the cache of the device and I had to change the name to the URI of each image but I do not know how to do it.
var cont = 0;
var id = response.data;
var indice = 1;
for (var i = 1; i <= 5 ; i++) {
var path = $scope.imagenes[i];
var options = {
fileKey: "file",
fileName: "ID-" + id + "-N-" + indice + ".jpg",
chunkedMode: false,
mimeType: "image/jpg"
};
indice++;
$cordovaFileTransfer.upload(server, path , options)
.then(function(result) {
cont++;
if(cont == 5)
$window.alert("TODO OK!");
}, function(err) {
$window.alert("ERROR" + JSON.stringify(err));
});
$scope.imagenes[i] = "";
}
- EDITED
I found a possible solution in another stackoverflow post. The problem is that the code is not adapted for angle and apparently this way it does not work!
function renameFile(src, callback) {
var d = new Date();
//find the FileEntry for the file on the device
window.resolveLocalFileSystemURL(src, function(fileEntry) {
//get the parent directory (callback gives a DirectoryEntry)
fileEntry.getParent(function(parent) {
//rename the file, prepending a timestamp.
fileEntry.moveTo(parent, d.getTime() + fileEntry.name, function(s) {
//Callback with the new URL of the file.
callback(s.nativeURL);
}, function(error) {
alert('Error on moving file!');
callback(src); //Fallback, use the src given
});
}, function(error) {
alert('Error on getting parent!');
callback(src); //Fallback
});
}, function(error) {
alert('Error on resolveLocalFileSystemURI!');
callback(src); //Fallback
});
}'