Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the path to the tmp folder and deleting contents on iOS phonegap/cordova

Tags:

file

ios

cordova

I'm having a hard time figuring out the phonegap/codrova file api docs.

How does one find the path to the application's tmp folder and then list/delete the contents without deleting the folder itself?

This specifically relates to the deletion of temporary image files created when I pull a photo from the device's image gallery.

like image 288
OliverJ90 Avatar asked Nov 18 '25 15:11

OliverJ90


1 Answers

This function uses the cordova file plugin to delete a specific file from the tmp folder

deleteFile: function(fileName) {

        var that = this;

        if (!fileName) {
            console.error("No fileName specified. File could not be deleted.");
            return false;
        }

        window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, function(fileSystem){ // this returns the tmp folder 

            // File found
            fileSystem.root.getFile(fileName, {create: false}, function(fileEntry){
                fileEntry.remove(function(success){
                    console.log(success);
                }, function(error){
                    console.error("deletion failed: " + error);
                });
            }, that.get('fail'));
        }, this.get('fail'));

    }

You could tweak it a bit to find all files first and remove them. Something like this

window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, function(fileSystem){ {
    var reader = fileSystem.root.createReader();
    reader.readEntries(function(entries) {

        var i;
        for (i = 0; i < entries.length; i++) {
            if (entries[i].name.indexOf(".png") != -1) {
               // delete stuff from above could go in here
            }
        }
    }, fail);    
 }
like image 143
matthisb Avatar answered Nov 20 '25 05:11

matthisb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!