Nested directory creator: Phonegap

后端 未结 5 1332
天涯浪人
天涯浪人 2020-12-16 03:50

How can I create a nested directory in Phonegap with this API?

fileSystem.root.getDirectory(\"Android/data/com.phonegap.myapp/dir_one/dir_two/\", {create:tru         


        
5条回答
  •  长情又很酷
    2020-12-16 03:56

    Just to add something to dhaval's function: it's not bullet proof for any browser compliant with Javascript Filesystem. If you use it with Chrome and your path is an empty string, it will fail, because apparently, with Chrome, using split() on an empty string returns a one element array, the one element being an epmty string itself, which then causes the directory creation to fail. I modified it in order to correct the problem (there also some other not related changes, for my own purposes):

    function createPath(fs, path, callback) {
        var dirs = path.split("/").reverse();
        var root = fs.root;
    
        var createDir = function(dir) {
            if (dir.trim()!="") {
                root.getDirectory(dir, {
                    create: true,
                    exclusive: false
                }, success, function(dir) {
                    error("failed to create dir " + dir);
                });
            } else {
                callback();
            }
        };
    
        var success = function(entry) {
            root = entry;
            if (dirs.length > 0) {
                createDir(dirs.pop());
            } else {
                callback();
            }
        };
    
        createDir(dirs.pop());
    }
    

提交回复
热议问题