說明
使用 chrome.fileSystem
API 建立、讀取、瀏覽及寫入使用者的本機檔案系統。透過這個 API,Chrome 應用程式就能讀取使用者選取的位置,以及將資料寫入其中。舉例來說,文字編輯器應用程式可以使用 API 讀取及寫入本機文件。所有失敗情形都會透過 chrome.runtime.lastError 通知。
權限
fileSystem
適用國家/地區
類型
AcceptOption
屬性
-
description
字串 選用
這是此選項的選填文字說明。如果沒有,系統會自動產生說明;通常包含有效的副檔名清單 (例如「text/html」可能會展開為「*.html, *.htm」)。
-
extensions
string[] 選填
可接受的擴充功能,例如「jpg」、「gif」或「crx」。
-
mimeTypes
string[] 選填
可接受的 MIME 類型,例如「image/jpeg」或「audio/*」。一種 MIMEType 或延伸模組必須包含至少一個有效元素。
ChooseEntryOptions
屬性
-
接受
AcceptOption[] 選用
這個檔案開啟工具的接受選項清單 (選用)。每個選項都會以不重複的群組方式呈現給使用者。
-
acceptsAllTypes
布林值 (選用)
除了 Accept 引數中指定的選項之外,是否接受所有檔案類型。預設值為 true。如未設定接受欄位或不含任何有效項目,系統將一律重設為 true。
-
acceptsMultiple
布林值 (選用)
是否接受多個檔案。這僅適用於 openFile 和 openWritableFile。如果設為 true,系統會呼叫 ChooseEntry 的回呼,並提供項目清單。否則,系統會使用單一項目呼叫。
-
suggestedName
字串 選用
系統會向使用者顯示建議檔案名稱,做為讀取或寫入的預設名稱。。
-
類型
要顯示的提示類型。預設值為「openFile」。
ChooseEntryType
列舉
"openFile"
提示使用者開啟現有檔案,並在成功時傳回 FileEntry。從 Chrome 31 以上版本開始,只要應用程式擁有「fileSystem」底下的「寫入」權限,即可寫入 FileEntry,否則 FileEntry 將成為唯讀狀態。
"openWritableFile"
提示使用者開啟現有檔案,並在成功時傳回可寫入的 FileEntry。如果應用程式沒有「fileSystem」底下的「寫入」權限,則使用這個類型的呼叫會失敗,並顯示執行階段錯誤。
"saveFile"
提示使用者開啟現有檔案或新檔案,並在成功時傳回可寫入的 FileEntry。如果應用程式沒有「fileSystem」底下的「寫入」權限,則使用這個類型的呼叫會失敗,並顯示執行階段錯誤。
"openDirectory"
提示使用者開啟目錄,並在成功時傳回 DirectoryEntry。如果應用程式沒有「fileSystem」底下的「目錄」權限,使用這種類型的呼叫將會失敗,並發生執行階段錯誤。如果應用程式擁有「fileSystem」底下的「寫入」權限,則傳回的 DirectoryEntry 將被寫入,否則將為唯讀。Chrome 31 版的新功能。
RequestFileSystemOptions
屬性
-
volumeId
字串
要求磁碟區的 ID。
-
可寫入
布林值 (選用)
要求的檔案系統是否可寫入。預設值為唯讀。
Volume
屬性
-
volumeId
字串
-
可寫入
boolean
VolumeListChangedEvent
屬性
-
volumes
數量[]
方法
chooseEntry()
chrome.fileSystem.chooseEntry(
options?: ChooseEntryOptions,
callback: function,
)
要求使用者選擇檔案或目錄。
參數
-
選項
-
回呼
功能
callback
參數如下所示:(entry?: Entry, fileEntries?: FileEntry[]) => void
-
項目
項目 (選填)
-
fileEntries
FileEntry[] 選用
-
getDisplayPath()
chrome.fileSystem.getDisplayPath(
entry: Entry,
callback?: function,
)
取得 Entry 物件的顯示路徑。顯示路徑是以本機檔案系統中檔案或目錄的完整路徑為基礎,但可能更易於顯示。
參數
-
項目
項目
-
回呼
函式選用
callback
參數如下所示:(displayPath: string) => void
-
displayPath
字串
-
傳回
-
Promise<string>
Chrome 117 以上版本Promise 僅支援 Manifest V3 以上版本,其他平台就必須使用回呼。
getVolumeList()
chrome.fileSystem.getVolumeList(
callback?: function,
)
傳回 requestFileSystem()
可用的磁碟區清單。必須具備 "fileSystem": {"requestFileSystem"}
資訊清單權限。僅適用於在資訊站工作階段中執行的資訊站應用程式。如果發生錯誤,系統不會定義 volumes
,並設定 chrome.runtime.lastError
。
傳回
-
Promise<Volume[] | undefined>
Chrome 117 以上版本Promise 僅支援 Manifest V3 以上版本,其他平台就必須使用回呼。
getWritableEntry()
chrome.fileSystem.getWritableEntry(
entry: Entry,
callback: function,
)
從另一個項目取得可寫入的項目。如果應用程式沒有「fileSystem」底下的「寫入」權限,則這個呼叫會失敗,並顯示執行階段錯誤。如果項目為 DirectoryEntry,如果應用程式沒有「fileSystem」底下的「目錄」權限,此呼叫就會失敗。
參數
-
項目
項目
-
回呼
功能
callback
參數如下所示:(entry: Entry) => void
-
項目
項目
-
isRestorable()
chrome.fileSystem.isRestorable(
id: string,
callback?: function,
)
傳回應用程式是否有權還原具有指定 ID 的項目。
參數
-
id
字串
-
回呼
函式選用
callback
參數如下所示:(isRestorable: boolean) => void
-
isRestorable
boolean
-
傳回
-
Promise<boolean>
Chrome 117 以上版本Promise 僅支援 Manifest V3 以上版本,其他平台就必須使用回呼。
isWritableEntry()
chrome.fileSystem.isWritableEntry(
entry: Entry,
callback?: function,
)
取得此項目是否可寫入。
參數
-
項目
項目
-
回呼
函式選用
callback
參數如下所示:(isWritable: boolean) => void
-
isWritable
boolean
-
傳回
-
Promise<boolean>
Chrome 117 以上版本Promise 僅支援 Manifest V3 以上版本,其他平台就必須使用回呼。
requestFileSystem()
chrome.fileSystem.requestFileSystem(
options: RequestFileSystemOptions,
callback?: function,
)
要求存取 options.volumeId
所代表磁碟區的檔案系統。如果將 options.writable
設為 True,就可以寫入檔案系統。否則將處於唯讀狀態。writable
選項需要資訊清單中的 "fileSystem": {"write"}
權限。僅適用於在資訊站工作階段中執行的資訊站應用程式。如果是手動啟動資訊站模式,使用中的應用程式視窗頂端會顯示確認對話方塊。如果發生錯誤,系統不會定義 fileSystem
,並設定 chrome.runtime.lastError
。
參數
-
回呼
函式選用
callback
參數如下所示:(fileSystem?: FileSystem) => void
-
fileSystem
檔案系統 (選用)
-
傳回
-
Promise<FileSystem | undefined>
Chrome 117 以上版本Promise 僅支援 Manifest V3 以上版本,其他平台就必須使用回呼。
restoreEntry()
chrome.fileSystem.restoreEntry(
id: string,
callback: function,
)
如果可以還原,則傳回具有指定 ID 的檔案項目。如果發生執行階段錯誤,這項呼叫就會失敗,並顯示執行階段錯誤。
參數
-
id
字串
-
回呼
功能
callback
參數如下所示:(entry: Entry) => void
-
項目
項目
-
retainEntry()
chrome.fileSystem.retainEntry(
entry: Entry,
)
傳回可傳遞至還原項目的 ID,以重新取得指定檔案項目的存取權。系統只會保留最近使用過的 500 個項目,其中保留項目和還原項目呼叫會計為使用。如果應用程式在「fileSystem」之下擁有「retainEntries」權限,系統就會無限期保留項目。否則,只有在應用程式執行期間和重新啟動時,項目才會保留。
參數
-
項目
項目
傳回
-
字串
活動
onVolumeListChanged
chrome.fileSystem.onVolumeListChanged.addListener(
callback: function,
)
在可用磁碟區清單變更時呼叫此方法。
參數
-
回呼
功能
callback
參數如下所示:(event: VolumeListChangedEvent) => void
-
event
-