chrome.fileSystem

說明

使用 chrome.fileSystem API 建立、讀取、瀏覽及寫入使用者的本機檔案系統。Chrome 應用程式可透過這個 API,在使用者選取的位置讀取及寫入資料。舉例來說,文字編輯器應用程式可以使用 API 讀取及寫入本機文件。所有失敗都會透過 chrome.runtime.lastError 通知。

權限

fileSystem

可用性

僅限前景

類型

AcceptOption

屬性

  • 說明

    字串 選填

    這是這個選項的選用文字說明。如果未提供,系統會自動產生說明,通常會包含有效的副檔名展開清單 (例如「text/html」可能會展開為「*.html, *.htm」)。

  • 額外資訊

    string[] 選填

    要接受的副檔名,例如「jpg」、「gif」、「crx」。

  • mimeTypes

    string[] 選填

    要接受的 MIME 類型,例如「image/jpeg」或「audio/*」。mimeTypes 或 extensions 必須包含至少一個有效元素。

ChooseEntryOptions

屬性

  • accepts

    AcceptOption[] 選填

    這個檔案開啟工具的接受選項清單 (選用)。每個選項都會以不重複群組的形式向使用者顯示。

  • acceptsAllTypes

    布林值 選填

    除了 accepts 引數中指定的選項外,是否接受所有檔案類型。預設值為 true。如果 accepts 欄位未設定或不含有效項目,則一律會重設為 true。

  • acceptsMultiple

    布林值 選填

    是否接受多個檔案。這項功能僅適用於 openFile 和 openWritableFile。如果設為 true,系統會使用項目清單呼叫 chooseEntry 的回呼。否則,系統會以單一項目呼叫。

  • suggestedName

    字串 選填

    系統建議的檔案名稱,會向使用者顯示為預設儲存名稱。這是選用項目,僅適用於「saveFile」。

  • 類型

    要顯示的提示類型。預設值為「openFile」。

ChooseEntryType

列舉

"openFile"
提示使用者開啟現有檔案,並在成功時傳回 FileEntry。從 Chrome 31 開始,如果應用程式在「fileSystem」下具有「write」權限,則 FileEntry 將可寫入;否則,FileEntry 將為唯讀。

"openWritableFile"
提示使用者開啟現有檔案,並在成功時傳回可寫入的 FileEntry。如果應用程式沒有「fileSystem」下的「write」權限,使用這種類型的呼叫會失敗,並顯示執行階段錯誤。

"saveFile"
提示使用者開啟現有檔案或新檔案,並在成功時傳回可寫入的 FileEntry。如果應用程式沒有「fileSystem」下的「write」權限,使用這種類型的呼叫會失敗,並顯示執行階段錯誤。

"openDirectory"
提示使用者開啟目錄,並在成功時傳回 DirectoryEntry。如果應用程式沒有「fileSystem」下的「directory」權限,使用這種類型的呼叫會失敗,並發生執行階段錯誤。如果應用程式在「fileSystem」下具有「write」權限,傳回的 DirectoryEntry 將可寫入,否則將為唯讀。Chrome 31 新功能。

RequestFileSystemOptions

Chrome 44 以上版本

屬性

  • volumeId

    字串

    所要求磁碟區的 ID。

  • 可寫入

    布林值 選填

    要求的檔案系統是否應可寫入。預設為唯讀。

Volume

Chrome 44 以上版本

屬性

  • volumeId

    字串

  • 可寫入

    布林值

VolumeListChangedEvent

Chrome 44 以上版本

屬性

方法

chooseEntry()

chrome.fileSystem.chooseEntry(
  options?: ChooseEntryOptions,
  callback: function,
)
: void

要求使用者選擇檔案或目錄。

參數

  • 選項
  • callback

    函式

    callback 參數如下:

    (entry?: Entry, fileEntries?: FileEntry[]) => void

    • entry

      項目 (選填)

    • fileEntries

      FileEntry[] 選填

getDisplayPath()

Promise
chrome.fileSystem.getDisplayPath(
  entry: Entry,
  callback?: function,
)
: Promise<string>

取得 Entry 物件的顯示路徑。顯示路徑是根據本機檔案系統上檔案或目錄的完整路徑,但可能會為了顯示目的而更易於閱讀。

參數

  • entry

    項目

  • callback

    函式 選填

    callback 參數如下:

    (displayPath: string) => void

    • displayPath

      字串

傳回

  • Promise<string>

    Chrome 117 以上版本

    Promise 僅適用於 Manifest V3 以上版本,其他平台則須使用回呼。

getVolumeList()

Promise Chrome 44+
chrome.fileSystem.getVolumeList(
  callback?: function,
)
: Promise<Volume[] | undefined>

傳回 requestFileSystem() 可用的磁碟區清單。必須具備 "fileSystem": {"requestFileSystem"} 資訊清單權限。僅適用於在資訊站工作階段中執行的資訊站應用程式。如果發生錯誤,系統不會定義 volumes,並會設定 chrome.runtime.lastError。

參數

  • callback

    函式 選填

    callback 參數如下:

    (volumes?: Volume[]) => void

傳回

  • Promise<Volume[] | undefined>

    Chrome 117 以上版本

    Promise 僅適用於 Manifest V3 以上版本,其他平台則須使用回呼。

getWritableEntry()

chrome.fileSystem.getWritableEntry(
  entry: Entry,
  callback: function,
)
: void

從其他項目取得可寫入的項目。如果應用程式沒有「fileSystem」下的「write」權限,這項呼叫會失敗,並顯示執行階段錯誤。如果項目是 DirectoryEntry,且應用程式沒有「fileSystem」下的「directory」權限,這項呼叫就會失敗。

參數

  • entry

    項目

  • callback

    函式

    callback 參數如下:

    (entry: Entry) => void

    • entry

      項目

isRestorable()

Promise
chrome.fileSystem.isRestorable(
  id: string,
  callback?: function,
)
: Promise<boolean>

傳回應用程式是否有權還原指定 ID 的項目。

參數

  • id

    字串

  • callback

    函式 選填

    callback 參數如下:

    (isRestorable: boolean) => void

    • isRestorable

      布林值

傳回

  • Promise<boolean>

    Chrome 117 以上版本

    Promise 僅適用於 Manifest V3 以上版本,其他平台則須使用回呼。

isWritableEntry()

Promise
chrome.fileSystem.isWritableEntry(
  entry: Entry,
  callback?: function,
)
: Promise<boolean>

取得這個項目是否可寫入。

參數

  • entry

    項目

  • callback

    函式 選填

    callback 參數如下:

    (isWritable: boolean) => void

    • isWritable

      布林值

傳回

  • Promise<boolean>

    Chrome 117 以上版本

    Promise 僅適用於 Manifest V3 以上版本,其他平台則須使用回呼。

requestFileSystem()

Promise Chrome 44+
chrome.fileSystem.requestFileSystem(
  options: RequestFileSystemOptions,
  callback?: function,
)
: Promise<FileSystem | undefined>

要求存取 options.volumeId 代表的磁碟區檔案系統。如果 options.writable 設為 True,檔案系統將可寫入。否則,該檔案會設為唯讀。writable 選項需要資訊清單中的 "fileSystem": {"write"} 權限。僅適用於在資訊站工作階段中執行的資訊站應用程式。如果是手動啟動 Kiosk 模式,確認對話方塊會顯示在執行中的應用程式視窗上方。如果發生錯誤,系統不會定義 fileSystem,並會設定 chrome.runtime.lastError。

參數

  • callback

    函式 選填

    callback 參數如下:

    (fileSystem?: FileSystem) => void

    • fileSystem

      FileSystem 選填

傳回

  • Promise<FileSystem | undefined>

    Chrome 117 以上版本

    Promise 僅適用於 Manifest V3 以上版本,其他平台則須使用回呼。

restoreEntry()

chrome.fileSystem.restoreEntry(
  id: string,
  callback: function,
)
: void

如果可以還原,則傳回具有指定 ID 的檔案項目。否則,這項呼叫會失敗,並發生執行階段錯誤。

參數

  • id

    字串

  • callback

    函式

    callback 參數如下:

    (entry: Entry) => void

    • entry

      項目

retainEntry()

chrome.fileSystem.retainEntry(
  entry: Entry,
)
: string

傳回可傳遞至 restoreEntry 的 ID,以便重新取得特定檔案項目的存取權。系統只會保留最近使用的 500 個項目,其中呼叫 retainEntry 和 restoreEntry 會計為使用。如果應用程式在「fileSystem」下具有「retainEntries」權限,系統會無限期保留項目。否則,系統只會在應用程式執行期間和重新啟動時保留項目。

參數

  • entry

    項目

傳回

  • 字串

事件

onVolumeListChanged

Chrome 44 以上版本
chrome.fileSystem.onVolumeListChanged.addListener(
  callback: function,
)

當可用磁碟區清單變更時呼叫。

參數