一個 Dart 庫,公開了 Web 平台的檔案系統存取 API,讓網路應用程式能夠安全地讀取和寫入使用者的本機檔案系統。它提供一種現代化、基於權限的檔案處理方式,與瀏覽器功能相符。該套件透過直覺的 Dart 界面簡化與檔案和資料夾的互動,提升需要檔案操作的網路應用體驗。它專為網路平台設計,並在可用時利用原生瀏覽器 API。
適用情境
直接從使用者的本機儲存讀取和寫入檔案
使用高級文件管理功能建構網頁應用
在Web應用程式中啟用拖放檔案操作
使用持久檔案存取建立離線優先的Web應用程式
允許使用者儲存檔案而不依賴下載提示
主要特色
與瀏覽器檔案系統存取 API 的直接整合
基於權限的檔案系統存取
支援讀取與寫入檔案及目錄
常見檔案操作的最小樣板程式碼
僅限 Web 的平台支援
適合對象
Web 開發人員建構以檔案為中心的應用程式
需要安全的用戶控制檔案存取的應用程式
針對具有檔案系統功能的現代瀏覽器的開發者
需要無縫檔案互動且無需伺服器往返的專案
尋求原生檔案系統行為的 Flutter Web 使用者
使用建議
僅適用於網頁平台
需要支援檔案系統存取 API 的現代瀏覽器
使用者權限必須授予檔案作業
對舊版瀏覽器的備用選項有限
不相容移動或桌面 Flutter 目標
原始 README
以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。
File System Access API
A Dart library to expose the File System Access API from web platform.
You can pick files and directories from user's file system to read, write, create, move and delete files/directories
from Dart web apps.
The web API (and this library) is experimental and is only supported on Chrome, Edge and Opera browsers for now.
It is not recommended to use this library in a production environment.
Features
This library reflects the web File System Access API using Dart types.
See a more detailed description on MDN Web Docs or keep reading to Usage section below.
Getting started
Install library in your Dart project:
$ dart pub add file_system_access_api
You're all set to play around with the API.
Note that in a cross-platform Flutter project, you can use this library only in a Web environment.
Use kIsWeb to prevent building/runtime errors and optimize build size.
Usage
You can reproduce example from the web API as much of the interfaces are transparent between this Dart library and the
JavaScript API.
Is browser supported
You can check if current browser supports the File System Access API with:
voidmain() {
if (!FileSystemAccess.supported) {
print("Browser does not support API.");
return;
}
print("Browser support API.");
}
Note that you must execute code samples below within main() as a result of a user gesture (e.g. click event) for
security reason. This part is omitted for brevity.
Open file(s)
You can ask a user to open file(s) with the method window.showOpenFilePicker() and access selected files like this:
voidmain() async {
try {
List<FileSystemFileHandle> handles = awaitwindow.showOpenFilePicker(
multiple: true,
excludeAcceptAllOption: true,
types: [FilePickerAcceptType(description: "Pictures", accept: {"image/png+jpg": [".png", ".jpg"]})],
startIn: WellKnownDirectory.pictures
);
for (final handle in handles) {
File file = await handle.getFile();
print("<file name='${handle.name}' size='${file.size} bytes' />");
// You can read content of a File using a FileReader, like any other File coming from 'dart:html'
}
} onAbortError {
print("User dismissed dialog or picked a file deemed too sensitive or dangerous.");
}
}
Save a file
You can ask a user where to save a file with the method window.showSaveFilePicker() and write in the selected file
like this:
voidmain() async {
try {
FileSystemFileHandle handle = awaitwindow.showSaveFilePicker(
suggestedName: "awesome.dart",
excludeAcceptAllOption: true,
types: [FilePickerAcceptType(description: "Dart files", accept: {"application/dart": [".dart"]})],
startIn: WellKnownDirectory.documents
);
FileSystemWritableFileStream stream = await handle.createWritable();
await stream.writeAsText(
"void main() {"" print('This is the way.');""}"
);
await stream.close();
// Note that data will be written on disk only after call to close() completed.
} onAbortError {
print("User dismissed dialog or picked a file deemed too sensitive or dangerous.");
} onNotAllowedError {
print("User did not granted permission to readwrite in this file.");
}
}
Open a directory
You can ask a user to pick a directory window.showDirectoryPicker() and recursively access files and directories like
this:
voidmain() async {
try {
FileSystemDirectoryHandle directory = awaitwindow.showDirectoryPicker(
// Use readwrite to ask permission and grant write access on files instead.
mode: PermissionMode.read
);
// List of handles in a directory emitted with a [Stream].// Listen periodically on [Stream] to reproduce a file system watch-like feature.awaitfor (FileSystemHandle handle in directory.values) {
if (handle.kind == FileSystemKind.file) {
print("<file name='${handle.name}' />");
} elseif (handle.kind == FileSystemKind.directory) {
print("<directory name='${handle.name}/' />");
// You can create, move and delete files/directories. See example/ for more on this.
}
}
} onAbortError {
print("User dismissed dialog or picked a directory deemed too sensitive or dangerous.");
}
}
Permissions
You can query / request permission on files and directories from the user. It allows you to write in files, modify a
directory structure, etc. Those methods are available per file/directory handle:
voidmain() async {
try {
final handles = awaitwindow.showOpenFilePicker(multiple: false);
final handle = handles.single;
var permission = await handle.queryPermission(mode: PermissionMode.readwrite);
if (permission != PermissionState.granted) {
print("Asking permission to read and write");
permission = await handle.requestPermission(mode: PermissionMode.readwrite);
if (permission != PermissionState.granted) {
print("read/write access refused, either prompt was dismissed or denied.");
return;
}
print("read/write access granted. 😀");
}
print("Write beautiful bytes. ✨");
} onAbortError {
print("User dismissed dialog or picked a file deemed too sensitive or dangerous.");
}
}
Note that current implementation of the File System Access API does not remember permissions across browser's sessions
when used with IndexedDB.
Star crbug.com/1011533 to be notified of work on persisting granted permissions.
Drag and Drop
You can get files/directories from a drag-n-drop event:
voidmain() {
final $area = document.querySelector("#area") asDivElement;
$area.addEventListener("dragover", (event) => event.preventDefault());
$area.addEventListener("drop", (event) async {
event.preventDefault();
final handles = awaitFileSystemAccess.fromDropEvent(event);
for (final handle in handles) {
print("<${handle.kind.name} name='${handle.name}' />");
}
});
}
Remove handle
Experimental and non-standard.
You can remove a file/directory directly from its handle, instead of using the handle of the parent directory:
FileSystemHandle handle;
if (handle.kind == FileSystemKind.file) {
final file = handle asFileSystemFileHandle;
print("handle is a file");
} elseif (handle.kind == FileSystemKind.directory) {
final directory = handle asFileSystemDirectoryHandle;
print("handle is a directory.");
}
How to compare FileSystemHandle(s)
DON'T compare using == operator or hashCode property:
FileSystemFileHandle a;
FileSystemFileHandle b;
if (a == b) {
print("will return false even when a and b represents the same file entry.");
}
DO compare using FileSystemHandle.isSameEntry:
voidmain() async {
FileSystemFileHandle a;
FileSystemFileHandle b;
if (await a.isSameEntry(b)) {
print("return true when a and b represents the same file entry.");
}
}
More
See examples in example/ folder to play with fun tools.
Origin Private File System
This is a storage endpoint private to the origin of the page. It contains files/directories within a "virtual disk"
chosen by the browser implementing this API. Files and directories may be written in a database or any other data
structure. You should not expect to find those files/directories as-this on the hard disk.
Get root directory
You can get a root directory of the origin private file system with:
Applies only within a Web Worker and Origin Private File System for now.
You can get an interface to synchronously read from and write to a file. This will create an exclusive lock on the file
associated with the file handle, until it is closed:
// Code executed within a Web Workervoidmain() async {
FileSystemDirectoryHandle root;
FileSystemFileHandle source = await root.getFileHandle("linux.iso");
FileSystemSyncAccessHandle src = await source.createSyncAccessHandle();
Uint8List buffer = Uint8List(src.getSize());
src.read(buffer);
src.close();
print(buffer);
}
Missing features
It's all good for now.
Known issues
You cannot store a FileSystemHandle into IndexedDB as-this. See issue #50621 for more. A workaround is implemented
in LightStorage within example/ folder.