Created
June 9, 2012 14:22
-
-
Save dhavaln/2901135 to your computer and use it in GitHub Desktop.
PhoneGap Nested Directory Create Sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> | |
<title>Insert title here</title> | |
<script type="text/javascript" charset="utf-8" src="lib/android/cordova-1.7.0.js"></script> | |
<script type="text/javascript"> | |
(function(){ | |
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("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()); | |
} | |
})(); | |
</script> | |
</head> | |
<body> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment