2012-04-19 7 views
4

下記のようにファイル入力とwebkitdirectoryを使用してディレクトリをロードしました。HTML5ファイルシステム - ディレクトリリーダーを使用してディレクトリを読み取る方法は?

<input id="file_input" type="file" webkitdirectory directory /> 

ディレクトリの選択後、ファイルサイズなどの情報を読み取ることができます。私の質問は、DirectoryReaderインターフェイスを使ってこのディレクトリを読む方法です。

以下のコードで試しましたが、成功しませんでした。 results.lengthはゼロになります。私は何か不足していますか?

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

助けてください。ディレクトリの内容を読み取るために

+2

は緑のは、あなたが受け入れるようにしたいの答えによって矢印を概説チェック。 –

答えて

14

:上記のコードで

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プロパティを使用します。

http://net.tutsplus.com/tutorials/html-css-techniques/toying-with-the-html5-filesystem-api/

+0

あなたの迅速な対応に感謝します。 – Joe

+1

'Documents'の代わりにルートディレクトリ名を入力しようとすると、FileError.NOT_FOUND_ERRエラーが発生します。 – Joe

+0

新しいコードを表示できますか? – fdicarlo

関連する問題