Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested directory creator: Phonegap

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.

like image 958
Huzoor Bux Avatar asked Jun 09 '12 12:06

Huzoor Bux


2 Answers

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

like image 112
dhaval Avatar answered Oct 24 '22 06:10

dhaval


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());
}
like image 40
Alexis Dufrenoy Avatar answered Oct 24 '22 07:10

Alexis Dufrenoy