辅助功能*

Jeff Swartz

Jeff Swartz

高级技术文档撰写工程师
Adobe

出版日期:
2008 年 2 月 28 日
用户级别:
中/高级
产品:
Adobe AIR

构建目录搜索应用程序

图 1 所示的文件搜索范例应用程序演示 Adobe AIR 的以下文件处理功能:

  • 异步读取文件, 以便在读取文件数据时可以运行其它进程
  • 从文件名中获取文件扩展名
  • 使用 File 对象中特定于平台的 nativePath 属性

文件搜索应用程序

图 1。通过本范例应用程序可以搜索特定文件。

注意: 本范例应用程序按原样提供, 用于教学目的。

要求

若要充分利用本篇文章, 您需要以下软件和文件:

Adobe AIR

Adobe AIR SDK

范例文件:

本范例应用程序包括以下文件:

  • index.html: 应用程序主文件, 在本文中讨论
  • application.xml: AIR 应用程序描述符文件
  • AIRAliases.js: AIR JavaScript 别名文件
  • SampleStyles.css: 定义应用程序中所用样式的 CSS 文件
  • AIR 图标文件范例

必备知识

应具备构建 HTML 应用程序的一般经验。有关使用此快速入门指南的详细信息, 请参阅用 HTML 构建快速入门范例应用程序

了解代码

以下各节讨论文件中 AIR 相关代码的工作方式。

设置要搜索的根目录

init() 方法将 folderPath 输入元素的文本值设置为预定义的路径 (用户的文档目录):

searchDir = air.File.documentsDirectory;
document.getElementById("folderPath").value = searchDir.nativePath;

File.documentsDirectory 是指向用户的文档目录 (如 Windows 中的“我的文档”) 的 File 对象, 而 nativePath 属性是该目录特定于平台的路径的字符串值。例如, 在 Windows 中, 此路径可能为:

C:\Documents and Settings\userName\My Documents

而在 Mac OS X 中, 此路径可能为:

/Users/userName/Documents

用户可以 (在 HTML 页的输入元素中) 编辑此值, 当用户单击 Search (搜索) 按钮时, search() 方法会检查路径是否有效, 如果无效, 则显示错误消息:

searchDir = new air.File(document.getElementById("folderPath").value);
if (!searchDir.isDirectory)
{
    alert("Invalid directory path.");
}

请注意 File 中 nativePath 属性与 url 属性之间的区别, 此处列出对同一个 directory File 对象的这种区别:

alert(directory.nativePath); // C:\Documents and Settings\swartz\My Documents 
alert(directory.url); // file:///C:/Documents%20and%20Settings/swartz/My%20Documents 

搜索目录中匹配的文件

主搜索进程以异步方式一次一条地列出目录中的内容。由此, 在运行时从文件系统 (异步) 获取目录列表信息时可以运行其它进程 (如结果列表)。

search() 方法为 dirListing 事件设置事件侦听器, listDirectoryAsync() 进程运行完毕时将调度该事件:

searchDir.addEventListener(air.FileListEvent.DIRECTORY_LISTING, dirListed);
searchDir.listDirectoryAsync();

dirListed() 方法处理当前目录中文件的列表。此列表由 dirListing 事件的 files 属性表示。该方法将 currentNodes 变量设置为此数组:

currentNodes = event.files; 

dirListed() 方法遍历此数组的每个成员, 以查看成员是否为目录。如果成员是目录, 则将该对象添加至 (当前正在检查的目录的) 当前子目录的列表。

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

遍历完毕之后, 将此数组的成员添加至要搜索的目录的主数组 (名为 directoryStack):

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

此时, 由于对当前目录的处理已完毕, 因此应用程序可以继续异步列出堆栈中的下一个目录 (如果还有目录):

var dir = directoryStack.pop();
if (dir == null) 
{
    document.getElementById('progress').innerHTML = ""; 
    // 没有其它目录要搜索。搜索完成。
} 
else 
{
    dir.addEventListener(air.FileListEvent.DIRECTORY_LISTING,
    dirListed);
    dir.listDirectoryAsync();
}

显示搜索结果

dirListed() 方法在遍历当前目录内容所构成的这个数组的各成员时, 将检查该名称是否与搜索模式相匹配, 如果匹配, 则将调用 writeFileResult() 方法, 该方法创建一个表格行 (tr 元素), 并将其添加至 HTML 文档的结果 div 元素:

if (node.isDirectory) 
{
    currentSubdirectories.push(currentNodes[i]);
}
if (node.name.search(pattern) > -1) 
{
    if (node.isDirectory) 
    {
        fileExtension = "";
    }
    else
    {
        fileExtension = node.extension;
    }
    var path = searchDir.getRelativePath(node);
    path = path.replace(/\//g, air.File.separator);
    writeFileResult(node.name, path, fileExtension);
}

如果文件没有扩展名, 则 File 对象的 extension 属性为空值, 而对于名称中含有点 (.) 字符的目录, 则将 extension 属性设置为名称中最后一个点字符之后的字符串部分。如果没有扩展名, 或 File 对象指向目录, 则上面代码中的前几行将 fileExtension 变量设置为空字符串 (否则, 将 fileExtension 设置为 File 对象的 extension 属性)。

writeFileResult() 方法根据匹配的文件或文件夹, 创建一个表格行 (tr 元素):

function writeFileResult(filename, path, extension) 
{
    var table = document.getElementById("results");
    var tr = document.createElement("tr");
    tr.setAttribute("class", "grid");
			
    var td = document.createElement("td");
    td.innerHTML = filename;
    tr.appendChild(td);
			
    td = document.createElement("td");
    td.style.width = "60%";
    td.innerHTML = path;
    tr.appendChild(td);
			
    td = document.createElement("td");
    td.style.width = "10%";
    td.innerHTML = extension;
    tr.appendChild(td);
			
    table.appendChild(tr); 
}

关于作者

Jeff Swartz 于 1992 年开始他在 Macromedia (现在为 Adobe Systems)的职业生涯。目前, 他是一名 Adobe AIR 技术文档撰写工程师。Jeff 拥有伊利诺斯大学乌尔班纳-香巴尼分校的计算机科学与数学专业学士学位, 并曾就读于爱丁堡大学人工智能系。旧金山湾地区附近的听众都曾“领略”过 Jeff 在长号演奏方面的艺术风采。他还曾是 Vienna Beef Ltd. 的 Big Frank (舞蹈高手)。