簡體   English   中英

Adobe Air HTML遞歸文件移至JSON

[英]Adobe Air HTML recursive file walk to JSON

我嘗試使用Adobe AIR HTML創建目錄列表。 輸出應為JSON文檔。

作為模板,我使用了以下node.js代碼: https : //stackoverflow.com/a/11194896/799692

文件漫游有效。 但是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>

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