Get list of files in a directory in cordova

橙三吉。 提交于 2019-12-06 09:39:00

问题


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

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.


回答1:


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


来源:https://stackoverflow.com/questions/32394003/get-list-of-files-in-a-directory-in-cordova

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!