Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
521 views
in Technique[技术] by (71.8m points)

ionic framework - Unable to delete file using cordova?

I am saving my file here : /storage/emulated/0/myApp/helloworld.wav

I am trying to delete this file

        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, successCallback, errorCallback)

        function successCallback(fs) {
            fs.root.getFile('/storage/emulated/0/myApp/helloworld.wav', {
                create: false
            }, function(fileEntry) {
                fileEntry.remove(function() {
                    alert('File removed.');
                }, errorCallback);
            }, errorCallback);
        }

        function errorCallback(error) {
            alert("ERROR: " + error.code)
        }

It does not delete the file and always returns error code 1 (not found). Can anybody help me in pointing what is wrong.

when I check from file manager,this is where my file is physically : /storage/emulated/0/myApp/helloworld.wav but it always return error code 1

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...