FileSystemWritableFileStream

基準線 2025
新推出

自 ⁨2025 年 9 月⁩起,此功能適用於最新裝置和瀏覽器版本。此功能可能不適用於較舊的裝置或瀏覽器。

安全上下文: 此功能僅在安全上下文(HTTPS)中可用,且支援此功能的瀏覽器數量有限。

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

FileSystemWritableFileStream 介面是 檔案系統 API 的一部分,它是一個帶有額外便捷方法的 WritableStream 物件,用於操作磁碟上的單個檔案。該介面透過 FileSystemFileHandle.createWritable() 方法訪問。

WritableStream FileSystemWritableFileStream

例項屬性

繼承其父介面 WritableStream 的屬性。

例項方法

繼承其父介面 WritableStream 的方法。

FileSystemWritableFileStream.write()

在當前檔案遊標偏移量處,將內容寫入呼叫該方法的該檔案。

FileSystemWritableFileStream.seek()

將當前檔案遊標偏移量更新為指定的(以位元組為單位的)位置。

FileSystemWritableFileStream.truncate()

將流關聯的檔案大小調整為指定的位元組數。

示例

以下非同步函式開啟“儲存檔案”選擇器,一旦選擇了檔案,它會返回一個 FileSystemFileHandle。從中,使用 FileSystemFileHandle.createWritable() 方法建立可寫流。

然後,文字字串將被寫入流,隨後流將被關閉。

js
async function saveFile() {
  // create a new handle
  const newHandle = await window.showSaveFilePicker();

  // create a FileSystemWritableFileStream to write to
  const writableStream = await newHandle.createWritable();

  // write our file
  await writableStream.write("This is my file content");

  // close the file and write the contents to disk.
  await writableStream.close();
}

以下示例展示了可以傳遞給 write() 方法的不同選項。

js
// just pass in the data (no options)
writableStream.write(data);

// writes the data to the stream from the determined position
writableStream.write({ type: "write", position, data });

// updates the current file cursor offset to the position specified
writableStream.write({ type: "seek", position });

// resizes the file to be size bytes long
writableStream.write({ type: "truncate", size });

規範

規範
檔案系統
# api-filesystemwritablefilestream

瀏覽器相容性

另見