FileSystemHandle:kind 属性

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

安全上下文: 此项功能仅在一些支持的浏览器安全上下文(HTTPS)中可用。

FileSystemHandle 接口的 kind 只读属性返回条目的类型。如果关联的条目是一个文件,则此值为 'file',否则为 'directory'。其用于在遍历目录内容时区分文件和目录。

可能为以下字符串值:

示例

下面的函数允许用户在文件选择器选择一个文件,然后测试返回的句柄是一个文件还是一个目录。

js
// 存放对文件句柄的引用
let fileHandle;

async function getFile() {
  // 打开文件选择器
  [fileHandle] = await window.showOpenFilePicker();

  if (fileHandle.kind === "file") {
    // 运行针对文件的代码
  } else if (fileHandle.kind === "directory") {
    // 运行针对目录的代码
  }
}

规范

Specification
File System Standard
# ref-for-dom-filesystemhandle-kind①

浏览器兼容性

BCD tables only load in the browser

参见