简体   繁体   English

Adobe Air HTML递归文件移至JSON

[英]Adobe Air HTML recursive file walk to JSON

I try to create a directory listing with Adobe AIR HTML. 我尝试使用Adobe AIR HTML创建目录列表。 Output should be a JSON Document. 输出应为JSON文档。

As a template I used this node.js code: https://stackoverflow.com/a/11194896/799692 作为模板,我使用了以下node.js代码: https : //stackoverflow.com/a/11194896/799692

The file walk works. 文件漫游有效。 But the JSON object only contain the last Folder. 但是JSON对象仅包含最后一个Folder。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Adobe Air directory listening to JSON</title>
    <script type="text/javascript" src="AIRAliases.js"></script>  <!-- adobe air framework -->  
    <script type="text/javascript" src="jquery-2.0.3.min.js"></script>
</head>
<body>

<script type="text/javascript">

var completePath = air.File.applicationDirectory.resolvePath("data").nativePath;

window.onload = function()
{   

    air.trace(JSON.stringify(dirTree(completePath), null, 4));

}



function dirTree(filename) 
{

    var currentFile = new air.File(filename);   
    var temp_path = currentFile.nativePath;                 


    info = {
        path: temp_path,
        name: currentFile.name
    };

    if (currentFile.isDirectory)
    {   
        if (currentFile.name !="." && currentFile.name !="..")
            {
            info.type = "folder";
            air.trace(Object.size(info));
            info.children = currentFile.getDirectoryListing().map(function(child) { 
                    return dirTree(child.nativePath);
            });
            }
        }
        else 
        {
            info.type = "file";         

        }   
    return info;                
} //end fn dirTree


// simple helper for object size
 Object.size = function(obj) {
    var size = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) size++;
    }
    return size;
};


</script>
</body>
</html>

Replace the map anonymous function with a function pointer: map匿名函数替换为函数指针:

function select_child(child) 
  { 
  return dirTree(child.nativePath);
  }

info.children = currentFile.getDirectoryListing().map(select_child);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM