获取cordova目录中的文件列表 [英] Get list of files in a directory in cordova

查看:626
本文介绍了获取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('文件列表创建')先执行,然后执行console.log('file.name'+ file.name)。我需要列出文件名,然后列出创建的信息,或者我需要知道列表何时完全创建。感谢提前。

Using the above code, I am getting the list of files. But the problem is in this code is "console.log('file list created')" is executed first and then the "console.log('file.name '+file.name)" are executed. I need to list the file names and then the list created information or I need to know when the list creates completely. Thanks in advance.

推荐答案

只需将console.log放在if语句中:

Just put the console.log in the if-statement:

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

这篇关于获取cordova目录中的文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆