小小菜鸟的web菜园子

web开发学习。好记性不如烂笔头。每天进步一点点!

导航

使用AIR创建目录搜索/文件搜索程序。

小小菜鸟翻译,不足之处请指正。谢谢!~
原文地址:http://www.adobe.com/devnet/air/flex/quickstart/directory_search.html

上图展示的AIR程序描述了以下功能细节:
1:异步读取文件,保证其他ACTIONSCRIPT程序可以在搜索文件的时候执行。
2:从文件名中获得文件的扩展名。
3:使用File对象的平台特定“本地路径(nativePath) ”属性。
  • FileSearch.zip (ZIP, 39K) 源文件
  • FileSearch.air (AIR, 628K) 安装程序文件

     程序解释:

    设置查询的根目录:
    小小菜鸟翻译,不足之处请指正。谢谢!~

    init() 函数设置了folderPath TextInput组件的text值为预定义搜索路径。

    folderPath.text=File.documentsDirectory.nativePath;

    File.documentsDirectory 是用户的“我的文档”目录.
    File.documentsDirectory在不同的平台上指定的目录不同。
    在Windows平台上:
    C:\Documents and Settings\userName\My Documents

    在Mac 平台上:
    /Users/userName/Documents

    用户可以通过设置TextInput组件的值改变搜索路径,当用户点击 搜索按钮,search() 函数判断用户输入的路径是否正确,如果路径不存在,则报错:

    var dir:File = new File(folderPath.text);
    if (!dir.isDirectory)
    {
        Alert.show("Invalid directory path.", "Error");
    }
    注意nativePath属性和  url 属性的不同之处:
    trace(directory.nativePath); // C:\Documents and Settings\swartz\My Documents
    trace(directory.url); // file:///C:/Documents%20and%20Settings/swartz/My%20Documents

    搜索指定目录中的匹配文件:
    小小菜鸟翻译,不足之处请指正。谢谢!~
    主要的搜索过程异步的列出了目录内容,一次一个。这个过程中,其他基于AS的集成可以执行。
    search()函数设置dirListing 事件的监听器,这个事件在getDirectoryListingAsync() 函数执行后触发。
    dir.addEventListener(FileListEvent.DIRECTORY_LISTING, dirListed);
    dir.getDirectoryListingAsync();


    dirListed()函数处理指定目录的文件列表。

    这个列表就是dirListing事件的files属性,dirListed()函数把这个属性(数组值)设置给currentNodes变量。

    currentNodes = event.files;

    dirListed()迭代执行,判断currentNodes数组中的值是否是文件夹,如果是文件夹,把当前值加入到subdirectories 数组中。

    node = currentNodes[i];
    if (node.isDirectory)
    {
        currentSubdirectories.push(currentNodes[i]);
    }

    dirListed()函数迭代执行完毕,把currentSubdirectories数组的值添加到即将被搜索的另外一个目录数组中directoryStack

    for (i = currentSubdirectories.length - 1; i > -1; i--)
    {
        directoryStack.push(currentSubdirectories[i]);
    }

    到此时,本目录的搜索已经完毕,开始搜索目录数组中directoryStack中的待搜索目录。

    var dir:File = directoryStack.pop();
    if (dir == null) {
        progress.visible = false;
        // There are no further directories to search. The search is completed.
    } else {
        dir.addEventListener(FileListEvent.DIRECTORY_LISTING, dirListed);
        dir.listDirectoryAsync();
    }

    显示搜索结果值:
    小小菜鸟翻译,不足之处请指正。谢谢!~
    dirListed() 函数迭代执行,搜索目录数组中指定的文件夹中的文件,如果搜到的文件的扩展名和我们指定的文件类型匹配,就把这个文件加入到一个ArrayCollection类型的变量resultData中。
    并且把这个变量设置为DataGrid组件resultsGrid的DataProvider属性。



     

  • posted on 2008-05-10 20:45  『小小菜鸟』  阅读(935)  评论(0编辑  收藏  举报