File System Access API
const dirHandle = await navigator.storage.getDirectory(); // 储存在 OPFS 中,不需要用户授权
const [fileHandle] = await window.showOpenFilePicker();
const handle = await window.showSaveFilePicker(opts);
// File handles are serializable,可以储存在 IndexedDB、传输,需要用户授权
fileHandle.requestPermission(opts)
const dirHandle = await window.showDirectoryPicker();
const newDirectoryHandle = await existingDirectoryHandle.getDirectoryHandle('My Documents', { create: true });
const newFileHandle = await newDirectoryHandle.getFileHandle('My Notes.txt', { create: true });
const path = await newDirectoryHandle.resolve(newFileHandle);
await directoryHandle.removeEntry('Old Stuff', { recursive: true });
之前叫 Native File System API
await window.chooseFileSystemEntries(); // 修改参数打开新文件,写文件后关闭时有保存窗口,需要用户手势
const file = await fileHandle.getFile();
const contents = await file.text();
async function writeFile(fileHandle, contents) {
// Create a writer (request permission if necessary).
const writer = await fileHandle.createWriter();
// Write the full length of the contents
await writer.write(0, contents);
// Close the file and write the contents to disk
await writer.close();
}
// 目前不允许后台(sw)访问
// 老的 File and Directory Entries API 虚拟文件系统
// 只有拖拽才能访问该文件系统