无法使用cordova删除文件? [英] Unable to delete file using cordova?

查看:132
本文介绍了无法使用cordova删除文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里保存我的文件: /storage/emulated/0/myApp/helloworld.wav



我要删除此文件

  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)
}

它不会删除该文件,并始终返回错误代码1 (找不到)。任何人都可以帮助我指出什么是错误。



当我从文件管理器检查,这是我的文件在物理上: / storage / /0/myApp/helloworld.wav 但总是返回错误代码1

解决方案

以下行可能是问题,
window.requestFileSystem(LocalFileSystem.PERSISTENT,0,successCallback,errorCallback)



一些帖子我读,它被提到requestfilsystem方法与LocalFileSystem.PERSISTENT参数将无法在Android中工作,除非设备根。



我使用 - window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory,successCallback,errorCallback); b
$ b

如果需要,我可以共享删除目录的示例代码及其中的文件。请告诉我。希望它有帮助。



这是根据请求的示例代码,

  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);
};

文件创建


$ b b

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

}

函数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){
//在相应目录中写入文件的逻辑
};

function errorHandler(e){
// handle error
}


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

解决方案

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
        }

这篇关于无法使用cordova删除文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆