檔案:webkitRelativePath 屬性

注意:此功能在 Web Workers 中可用。

webkitRelativePathFile 介面的一個只讀屬性,它包含一個字串,用於指定檔案相對於使用者在具有 webkitdirectory 屬性的 <input> 元素中選擇的目錄的路徑。

一個字串,包含檔案相對於使用者選擇的祖先目錄的路徑。

示例

在此示例中,會顯示一個目錄選擇器,允許使用者選擇一個或多個目錄。當 change 事件發生時,將生成並顯示所選目錄層次結構中包含的所有檔案的列表。

HTML

html
<input type="file" id="file-picker" name="fileList" webkitdirectory multiple />
<output id="output"></output>

JavaScript

js
const output = document.getElementById("output");
const filePicker = document.getElementById("file-picker");

filePicker.addEventListener("change", (event) => {
  const files = event.target.files;

  for (const file of files) {
    output.textContent += `${file.webkitRelativePath}\n`;
  }
});

結果

規範

規範
File and Directory Entries API
# dom-file-webkitrelativepath

瀏覽器相容性

另見