获取科尔多瓦目录中的文件列表
Get list of files in a directory in cordova
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onFileSystemFail);
}
function onFileSystemSuccess(fileSystem) {
var directoryEntry = fileSystem.root;
directoryEntry.getDirectory("MyDirectory", {create: true, exclusive: false}, onDirectorySuccess, onDirectoryFail);
}
function onDirectorySuccess(parent) {
var directoryReader = parent.createReader();
directoryReader.readEntries(success, fail);
}
function fail(error) {
alert("Failed to list directory contents: " + error.code);
}
function success(entries) {
if (entries.length == 0)
console.log("No Records");
else
{
for (var i = 0; i < entries.length; i++) {
entries[i].file(function (file) {
console.log("file.name " + file.name);
})
}
}
console.log('file list created');
}
function onDirectoryFail(error) {
alert("Unable to create new directory: " + error.code);
}
function onFileSystemFail(evt) {
console.log(evt.target.error.code);
}
使用上面的代码,我得到了文件列表。但问题在于这段代码是先执行 "console.log('file list created')" 然后执行 "console.log('file.name '+file.name)"。我需要列出文件名,然后列出列表创建的信息,或者我需要知道列表何时完全创建。提前致谢。
只需将 console.log 放在 if 语句中:
function success(entries) {
if (entries.length == 0)
console.log("No Records");
else
{
for (var i = 0; i < entries.length; i++) {
entries[i].file(function (file) {
console.log("file.name " + file.name);
})
}
console.log('file list created');
}
}
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onFileSystemFail);
}
function onFileSystemSuccess(fileSystem) {
var directoryEntry = fileSystem.root;
directoryEntry.getDirectory("MyDirectory", {create: true, exclusive: false}, onDirectorySuccess, onDirectoryFail);
}
function onDirectorySuccess(parent) {
var directoryReader = parent.createReader();
directoryReader.readEntries(success, fail);
}
function fail(error) {
alert("Failed to list directory contents: " + error.code);
}
function success(entries) {
if (entries.length == 0)
console.log("No Records");
else
{
for (var i = 0; i < entries.length; i++) {
entries[i].file(function (file) {
console.log("file.name " + file.name);
})
}
}
console.log('file list created');
}
function onDirectoryFail(error) {
alert("Unable to create new directory: " + error.code);
}
function onFileSystemFail(evt) {
console.log(evt.target.error.code);
}
使用上面的代码,我得到了文件列表。但问题在于这段代码是先执行 "console.log('file list created')" 然后执行 "console.log('file.name '+file.name)"。我需要列出文件名,然后列出列表创建的信息,或者我需要知道列表何时完全创建。提前致谢。
只需将 console.log 放在 if 语句中:
function success(entries) {
if (entries.length == 0)
console.log("No Records");
else
{
for (var i = 0; i < entries.length; i++) {
entries[i].file(function (file) {
console.log("file.name " + file.name);
})
}
console.log('file list created');
}
}