如何访问特定文件夹以获取我的扩展名中的文件名

时间:2017-03-24 20:52:31

标签: javascript google-chrome google-chrome-extension directory

所以我需要在我的" db"中获取文件的名称。文件夹,我后来需要在我的扩展中使用。我搜索了如何执行此操作,我可以从我的根扩展文件夹中获取所有文件名,但不能从其他任何文件名中获取。

这是从我在此问题中找到的扩展程序文件夹中提供所有文件名的代码:How do I get a list of filenames in a subfolder of a Chrome extension?

chrome.runtime.getPackageDirectoryEntry(function(directoryEntry) {
var directoryReader = directoryEntry.createReader();
// List of DirectoryEntry and/or FileEntry objects.
var filenames = [];
(function readNext() {
    directoryReader.readEntries(function(entries) {
        if (entries.length) {
            for (var i = 0; i < entries.length; ++i) {
                filenames.push(entries[i].name);
            }
            readNext();
        } else {
            // No more entries, so all files in the directory are known.
            // Do something, e.g. print all file names:
            console.log(filenames);
        }
    });
})();
});

这是应该做我想要的代码,但我无法弄清楚如何在这里实现它

directoryEntry.getDirectory('_locales', {}, function(subDirectoryEntry) {
var directoryReader = subDirectoryEntry.createReader();
// etc.. same code as in previous snippet.
});

1 个答案:

答案 0 :(得分:0)

我需要的代码就是这个。

chrome.runtime.getPackageDirectoryEntry(function(directoryEntry) {
    directoryEntry.getDirectory('index', {}, function(subDirectoryEntry) {
        var directoryReader = subDirectoryEntry.createReader();
        // List of DirectoryEntry and/or FileEntry objects.
        var filenames = [];
        (function readNext() {
            directoryReader.readEntries(function(entries) {
                if (entries.length) {
                    for (var i = 0; i < entries.length; ++i) {
                        filenames.push(entries[i].name);
                    }
                    readNext();
                } else {
                    // No more entries, so all files in the directory are known.
                    // Do something, e.g. print all file names:
                    console.log(filenames);
                }
            });
        })();
    });
});