Cordoba delete file

I have a working application that creates a file. I am looking for a way to delete files from a cordova application after several hours of work. I can not 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);
});

}

Regards.

+4
source share
2 answers

This works for me:

function del() {

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

        dir.getFile("log1.txt", {create: false}, function (fileEntry) {
            fileEntry.remove(function (file) {
                alert("fichier supprimer");
            }, function () {
                alert("erreur suppression " + error.code);
            }, function () {
                alert("fichier n'existe pas");
            });
        });


    });

}

thanks

+7
source

It also works using requestFileSysteminstead resolveLocalFileSystemURL:

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {
    fs.root.getFile('test.txt', {
        create: false,
        exclusive: false
    }, function(fileEntry) {
        fileEntry.remove(function (file) {
            // File deleted successfully
        }, function (err) {
            console.log(err); // Error while removing File
        });

    }, function(err) {
        console.log(err)  // Error while requesting File.
    });
}, function(err) {
    console.log(err)  // Error while requesting FS
});
0
source

Source: https://habr.com/ru/post/1616177/


All Articles