HTML5文件系统-如何使用目录读取器读取目录? [英] HTML5 File System - How to read directories using directory reader?
问题描述
我使用文件输入和webkit目录加载了一个目录,如下所述.
I loaded a directory using file input and webkitdirectory as mentioned below.
<input id="file_input" type="file" webkitdirectory directory />
选择目录后,我可以读取文件大小和其他信息.我的问题是如何使用DirectoryReader界面读取此目录.
After directory selected I can read file size and other information. My question is how to read this directory using DirectoryReader interface.
我尝试使用以下代码,但未成功. results.length变为零.我想念什么吗?
I tried with below code, but not success. results.length becomes zero. Am I missing anything?
window.requestFileSystem(TEMPORARY, 1024*1024 /*1MB*/, function(fs) {
var dirReader = fs.root.createReader();
var entries = [];
// Call the reader.readEntries() until no more results are returned.
var readEntries = function() {
dirReader.readEntries(function(results) {
// If no more results are returned, we're done.
if (!results.length) {
// Sort list by name of entry.
entries.sort(function(a, b) {
return a.name < b.name ? -1 :
b.name < a.name ? 1 : 0;
});
// listResults(entries); // Render the list.
} else {
// Add in these results to the current list.
entries = entries.concat(toArray(results));
readEntries();
}
}, errorHandler);
};
readEntries(); // Start reading the directory.
}, errorHandler);
感谢您的帮助.
推荐答案
要读取目录的内容:
fs.root.getDirectory('Documents', {}, function(dirEntry){
var dirReader = dirEntry.createReader();
dirReader.readEntries(function(entries) {
for(var i = 0; i < entries.length; i++) {
var entry = entries[i];
if (entry.isDirectory){
console.log('Directory: ' + entry.fullPath);
}
else if (entry.isFile){
console.log('File: ' + entry.fullPath);
}
}
}, errorHandler);
}, errorHandler);
在上面的代码中,isDirectory和isFile属性用于分别获取目录和文件的不同输出.另外,我们使用fullPath属性来获取条目的完整路径,而不仅仅是其名称.
In the code above, the isDirectory and isFile properties are used in order to obtain a different output for directories and files, respectively. Additionally, we use the fullPath property in order to get the full path of the entry, instead of its name only.
来自: http://net.tutsplus.com/tutorials/html-css-techniques/toying-with-the-html5-filesystem-api/
这篇关于HTML5文件系统-如何使用目录读取器读取目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!