Unable to delete file using cordova?

纵然是瞬间 提交于 2019-11-28 01:42:54

I feel the below line could be the issue, "window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, successCallback, errorCallback)"

In some of the posts i read, it was mentioned that requestfilsystem method along with LocalFileSystem.PERSISTENT argument will not work in android unless the device is rooted.

I made it work using - "window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory,successCallback, errorCallback);"

If required, i can share the sample code for deleting directory along with files in it. Please let me know. Hope it helps.

Here is the sample code as per the request,

function clearDirectory() {

    if (sessionStorage.platform.toLowerCase() == "android") {
        window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory, onFileSystemDirSuccess, fail);
    } else {
        //for ios
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemDirSuccess, fail);
    }
};

function onFileSystemDirSuccess(fileSystem) {
    var entry = "";
    if (sessionStorage.platform.toLowerCase() == "android") {
        entry = fileSystem;
    } else {
        //for ios
        entry = fileSystem.root;
    }
    entry.getDirectory("Folder_Name", {
            create: true,
            exclusive: false
        },
        function(entry) {
            entry.removeRecursively(function() {
                console.log("Delete successful !!!");
            }, fail);
        }, getDirFail);
};

function getDirFail(error) {
    alert("getDirFail - " + error.code);
};

function fail(error) {
    alert("fail - " + error.code);
};

File creation:

function writeFile() {
            if (sessionStorage.platform.toLowerCase() == "android") {
                window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory, onFileSystemSuccess, onError);
            } else {
                window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onError);
            }

        }    

        function onError(e) {
            alert("onError");
        };

        function onFileSystemSuccess(fileSystem) {
            var entry = "";
            if (sessionStorage.platform.toLowerCase() == "android") {
                entry = fileSystem;
            } else {
                entry = fileSystem.root;
            }
            entry.getDirectory("Folder_Name", {
                create: true,
                exclusive: false
            }, onGetDirectorySuccess, onGetDirectoryFail);
        };

        function onGetDirectorySuccess(dir) {
            dir.getFile(filename, {
                create: true,
                exclusive: false
            }, gotFileEntry, errorHandler);
        };

        function gotFileEntry(fileEntry) {
            // logic to write file in respective directory
        };

        function errorHandler(e) {
            // handle error
        }

Perhaps it's best if you use a plugin with native capabilities, which will save files where they should be saved and you will be guaranteed read and write access.

You can check it out here: cordova-plugin-file

I don't know in what kind of device you are trying to use that code. In that page you can see all the different filesystem paths to different OS (Android, iOS, Blackberry, etc).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!