Nested directory creator: Phonegap

大城市里の小女人 提交于 2019-12-29 05:25:12

问题


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:true}, gotDir, onError);

I am using Phonegap 1.8.0 in Android 2.2.


回答1:


This function will help you create nested dirs.

document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
    console.log("device is ready");
    window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem;
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}

function fail() {
    console.log("failed to get filesystem");
}

function gotFS(fileSystem) {
    window.FS = fileSystem;

    var printDirPath = function(entry){
        console.log("Dir path - " + entry.fullPath);
    }

    createDirectory("dhaval/android/apps", printDirPath);
    createDirectory("this/is/nested/dir", printDirPath);
    createDirectory("simple_dir", printDirPath);
}

function createDirectory(path, success){
    var dirs = path.split("/").reverse();
    var root = window.FS.root;

    var createDir = function(dir){
        console.log("create dir " + dir);
        root.getDirectory(dir, {
            create : true,
            exclusive : false
        }, successCB, failCB);
    };

    var successCB = function(entry){
        console.log("dir created " + entry.fullPath);
        root = entry;
        if(dirs.length > 0){
            createDir(dirs.pop());
        }else{
            console.log("all dir created");
            success(entry);
        }
    };

    var failCB = function(){
        console.log("failed to create dir " + dir);
    };

    createDir(dirs.pop());
}

For full example check this gist




回答2:


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



回答3:


This code should do what you want:

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(filesys) {
    filesys.root.getDirectory("story_repository", {create: true, exclusive: false}, function(dirEntry) {
        dirEntry.getDirectory("dir_name", {create: true, exclusive: false}, function(dirName) {
            dirName.getDirectory("img", {create: true, exclusive: false}, function(imgDir) {
                console.log("img creation worked");
            }, function(error) {
                console.log("create img failed");
            });
            dirName.getDirectory("res", {create: true, exclusive: false}, function(imgDir) {
                console.log("res creation worked");
            }, function(error) {
                console.log("create res failed");
            });
        }, function(error) {
            console.log("create dir_name failed");
        })
    }, function(error) {
        console.log("create story repository failed");
    });
}, function(error) {
    console.log("request file system failed");
});



回答4:


I'm using this:

function recursiveGetFile(root, path, opts, success, fail) {
    function dir(entry) {
        var name = path.shift();
        if (path.length > 0)
            entry.getDirectory(name, opts, dir, fail);
        else
            entry.getFile(name, opts, success, fail);
    }
    path = path.split('/');
    dir(root);
}, fail);



回答5:


There is a simple file manager for cordova-phoengap, with with you can do this and much more:

https://github.com/torrmal/cordova-simplefilemanagement

You can recursively create directories:

//CREATE A DIRECTORY RECURSIVELY as simple as:

new DirManager().create_r('folder_a/folder_b',Log('created successfully'));

Let me know if it helps



来源:https://stackoverflow.com/questions/10961000/nested-directory-creator-phonegap

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