Reputation: 816
I am new to ionic . I need to remove all the files in my local drive location. I have tried below query . But it's not working
$cordovafile.removeFile(cordova.file.documentsDirectory,"filename").then(function(success){
console log("file removed"),
function error(error){
console log("Error")
});}
Please suggest your solution.
Upvotes: 2
Views: 3872
Reputation: 181
basically you list files within a folder and then create a promises' array that delete each single file. And when deleted all of them, invoke the .then() allowing you to go on with your stuffs.
the present solution works with ionic 1.x (possibly also 2+, don't know) after having done:
install cordova plugin add cordova-plugin-file.
I use it with ionic-native wrapper I suggest you to create a service
function FileSystemSrv($cordovaFile, $q)
{
var service = {};
service.data_storage_root = "";
service.resolved_data_storage_root = "";
//====================================================
service.init = function(data_storage_root)
{
service.data_storage_root = data_storage_root;
service.resolved_data_storage_root = cordova.file[service.data_storage_root];
return service.resolved_data_storage_root;
};
//--------------------------------------------------------------
service.getExtension = function(fullname)
{
var arr = fullname.split(".");
return arr[arr.length-1];
};
//-------------------------------------------------------------------
// invoke the then(), returning 1 or 0, instead of invoking the success or error callbacks
service.existFile = function(relative_path)
{
return $cordovaFile.checkFile(service.resolved_data_storage_root, relative_path)
.then(function (success){
return 1;
})
.catch(function (error){
return $q.resolve(0);
});
};
//--------------------------------------------------------------
//return all the files contained in a folder, belonging to the [valid_extensions] formats.
service.listFilesInDir = function(relative_path, valid_extensions)
{
var len_ext = 0;
if(valid_extensions != null) len_ext = valid_extensions.length;
return $cordovaFile.listDir(service.resolved_data_storage_root, relative_path)
.then(function(dirs)
{
var len = dirs.length;
var arr = [];
var cnt = 0;
for (d=0; d<len; d++)
{
if (!dirs[d].isDirectory)
{
var insert = false;
if(len_ext)
{
// filter input files: show only some extensions
var ext = service.getExtension(dirs[d].name);
for (e=0; e<valid_extensions.length; e++)
{
if( ext == valid_extensions[e])
{
insert = true;
break;
}
}
}
else insert = true;
if(insert)
{
arr[cnt] = dirs[d].name;
cnt++;
}
}
}
return arr;
})
.catch(function(error){
console.log("FileSystemSrv::listFilesInDir" + error.message);
return $q.reject(error);
});
};
service.deleteFile = function(relative_path)
{
return service.existFile(relative_path)
.then(function(exist){
if (exist) return $cordovaFile.removeFile(service.resolved_data_storage_root, relative_path);
else return 1;
})
.catch(function(error){
console.log("FileSystemSrv::deleteFile" + JSON.stringify(error));
return $q.reject(error);
});
};
service.deleteFilesInFolder = function(relative_path, valid_extensions)
{
return service.listFilesInDir(relative_path, valid_extensions)
.then(function(files)
{
var subPromises = [];
for (var f=0; f<files.length; f++)
{
(function(fname)
{
var subPromise = service.deleteFile(fname)
subPromises.push(subPromise);
})(relative_path + "/" + files[f]);
}
return $q.all(subPromises);
})
.then(function()
{
return $q.defer().resolve(1);
})
.catch(function(error)
{
console.log("FileSystemSrv::deleteFilesInFolder" + JSON.stringify(error));
$q.reject(error);
});
};
return service;
}
main_module.service('FileSystemSrv', FileSystemSrv);
and then in your controller (inject the service) create this method:
$scope.deleteSession = function()
{
FileSystemSrv.init("externalRootDirectory");
return FileSystemSrv.deleteFilesInFolder($scope.relpath, ["wav", "jpg"]) // relpath = "AFolder/asubfolder" a subfolder of externalRootDirectory
.then(function()
{
// do something
})
.catch(function(error){
alert(error.message);
});
};
Upvotes: 0
Reputation: 1531
install cordova plugin add cordova-plugin-file
then inject $cordovaFile
to your controller some thing like this
app.controller('MyCtrl', function($scope, $cordovaFile) {}
then try
$cordovaFile.removeFile(path,file)
.then(function (success) {
// success
}, function (error) {
// error
});
removeFile only remove single file instead of that you can remove whole directory itself by $cordovaFile.removeDir(path,directory)
Upvotes: 3