Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cordova remove file

Tags:

cordova

I have a working app that creates a file. I'm looking for a way to remove files from a cordova app after some hours of work. I can't seem to make it work.

here is the code for creating and deleting files :

    function crea(){
        alert(cordova.file.externalDataDirectory);
    window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function(dir) {
            alert("got main dir",dir);
            dir.getFile("log1.txt", {create:true}, function(file) {
                alert("got the file", file);
                            
            });
        });
            }



        function del(){
    var relativeFilePath = cordova.file.dataDirectory;
    var filename = "log1.txt"; 
    alert(relativeFilePath);
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){
      //  alert(fileSystem.root.fullPath);
        fileSystem.root.getFile(relativeFilePath + filename, {create:false}, function(fileEntry){
            fileEntry.remove(function(file){
                alert("File removed!");
            },function(){
                alert("error deleting the file " + error.code);
                });
            },function(){
                alert("file does not exist");
            });
        },function(evt){
            alert(evt.target.error.code);
    });
    
    }

Best Regards.

like image 641
Maher Tliba Avatar asked Nov 17 '15 09:11

Maher Tliba


1 Answers

I got it works :

function del() {

    window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function (dir) {

        dir.getFile("log1.txt", {create: false}, function (fileEntry) {
            fileEntry.remove(function (file) {
                alert("file removed!");
            }, function (error) {
                alert("error occurred: " + error.code);
            }, function () {
                alert("file does not exist");
            });
        });
    });

}

Thanks

like image 83
Maher Tliba Avatar answered Sep 19 '22 14:09

Maher Tliba