FileSystemHandle: thuộc tính kind

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2023.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Note: This feature is available in Web Workers.

Thuộc tính chỉ đọc kind của giao diện FileSystemHandle trả về kiểu của mục nhập. Giá trị là 'file' nếu mục nhập liên quan là một file hoặc 'directory' nếu là thư mục. Thuộc tính này được dùng để phân biệt file với thư mục khi lặp qua nội dung của một thư mục.

Giá trị

Một chuỗi có thể là:

Ví dụ

Hàm sau cho phép người dùng chọn một file từ bộ chọn file rồi kiểm tra xem handle trả về là file hay thư mục.

js
// lưu tham chiếu tới file handle của chúng ta
let fileHandle;

async function getFile() {
  // mở bộ chọn file
  [fileHandle] = await window.showOpenFilePicker();

  if (fileHandle.kind === "file") {
    // chạy mã dành cho file
  } else if (fileHandle.kind === "directory") {
    // chạy mã dành cho thư mục
  }
}

Thông số kỹ thuật

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

Tương thích trình duyệt

Xem thêm