问题
can you please tell me how to create folder in directory in android (SDCard) and in IOS.?I also need to count number of folder in same directory. I have directory File://SDCard/test . here one by one create folder .During application launch i need to count number of folder in dame directory .
What is directory of IPhone ?
回答1:
Take a look at the Cordova File API which is based on the W3C File API. It is used to read, write and navigate file system hierarchies.
List Files and Folders
In order to list/count the lists and files inside a directory you have to use the DirectoryReader object.
Supported Platforms:
- Android
- BlackBerry WebWorks (OS 5.0 and higher)
- iOS
- Windows Phone 7 and 8
- Windows 8
Example:
function success(entries) {
var i;
for (i=0; i<entries.length; i++) {
console.log(entries[i].name);
}
}
function fail(error) {
alert("Failed to list directory contents: " + error.code);
}
// Get a directory reader
var directoryReader = dirEntry.createReader();
// Get a list of all the entries in the directory
directoryReader.readEntries(success,fail);
Create Directory
In order to create a directory you have to use the DirectoryEntry object. This object contains the getDirectory method which creates or looks up a directory.
Supported Platforms:
- Android
- BlackBerry WebWorks (OS 5.0 and higher)
- iOS
- Windows Phone 7 and 8
- Windows 8
Example:
<!DOCTYPE html>
<html>
<head>
<title>Local File System Example</title>
<script type="text/javascript" charset="utf-8" src="cordova-x.x.x.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for Cordova to load
document.addEventListener("deviceready", onDeviceReady, false);
// Cordova is ready
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onFileSystemFail);
}
function onFileSystemSuccess(fileSystem) {
console.log(fileSystem.name);
var directoryEntry = fileSystem.root;
directoryEntry.getDirectory("newDir", {create: true, exclusive: false}, onDirectorySuccess, onDirectoryFail)
}
function onDirectorySuccess(parent) {
console.log(parent);
}
function onDirectoryFail(error) {
alert("Unable to create new directory: " + error.code);
}
function onFileSystemFail(evt) {
console.log(evt.target.error.code);
}
</script>
</head>
<body>
<h1>Example</h1>
<p>Local File System</p>
</body>
</html>
I hope this helps.
来源:https://stackoverflow.com/questions/17119372/how-to-create-folder-in-directory-in-iphone-and-android