FileSystemHandle:kind 属性

Baseline 2023
Newly available

Since March 2023, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

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

备注: 此特性在 Web Worker 中可用。

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
# ref-for-dom-filesystemhandle-kind①

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
kind

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support

参见