File System Access API


const [fileHandle] = await window.showOpenFilePicker();
const handle = await window.showSaveFilePicker(opts);
File handles are serializable,可以储存,传输 // 不是 json 支持的类型???
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 });
await navigator.storage.getDirectory(); // 基于源的虚拟文件系统

之前叫 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:
  • 在沙盒中操作文件
  • 已被弃用
  • 可用 IndexedDB 替代