FileSystemDirectoryEntry

FileSystemDirectoryEntry 介面是 File and Directory Entries API 的一部分,代表檔案系統中的一個目錄。它提供了可以用於訪問和操作目錄中的檔案,以及訪問目錄內條目的方法。

FileSystemEntry FileSystemDirectoryEntry

基本概念

您可以透過呼叫 getDirectory() 來建立一個新目錄。如果要建立子目錄,請按順序建立每個子目錄。如果您嘗試使用包含尚未存在的父目錄的完整路徑建立目錄,則會返回錯誤。因此,請透過在建立父目錄後遞迴新增新路徑來建立目錄結構。

示例

在以下程式碼片段中,我們建立了一個名為“Documents”的目錄。

js
// Taking care of the browser-specific prefixes.
window.requestFileSystem =
  window.requestFileSystem || window.webkitRequestFileSystem;
window.directoryEntry = window.directoryEntry || window.webkitDirectoryEntry;

// …

function onFs(fs) {
  fs.root.getDirectory(
    "Documents",
    { create: true },
    (directoryEntry) => {
      // directoryEntry.isFile === false
      // directoryEntry.isDirectory === true
      // directoryEntry.name === 'Documents'
      // directoryEntry.fullPath === '/Documents'
    },
    onError,
  );
}

// Opening a file system with temporary storage
window.requestFileSystem(TEMPORARY, 1024 * 1024 /* 1MB */, onFs, onError);

例項屬性

此介面本身沒有屬性,但會從其父介面 FileSystemEntry 繼承屬性。

例項方法

此介面從其父介面 FileSystemEntry 繼承方法。

createReader()

建立一個 FileSystemDirectoryReader 物件,該物件可用於讀取此目錄中的條目。

getDirectory()

返回一個 FileSystemDirectoryEntry 物件,該物件代表位於給定路徑的目錄,該路徑相對於呼叫該方法的目錄。

getFile()

根據相對於呼叫該方法的目錄的路徑,返回一個 FileSystemFileEntry 物件,該物件代表位於目錄層次結構內的檔案。

removeRecursively() 已棄用 非標準

遞迴刪除目錄及其所有內容,依次遍歷其所有後代檔案和目錄的子樹。

規範

規範
File and Directory Entries API
# api-directoryentry

瀏覽器相容性

另見