`title: File System Access 目录选择与批量持久化``categories: Web 开发/前端/数据管理``keywords: File System Access API,showDirectoryPicker,目录选择,OPFS,批量``description: 使用 showDirectoryPicker 选择目录并批量读取文件,桥接到 OPFS 或 IndexedDB 进行持久化与索引管理。`选择与遍历async function importDir() {
const dir = await window.showDirectoryPicker();
for await (const [name, handle] of dir.entries()) {
if (handle.kind === 'file') {
const file = await handle.getFile();
const root = await navigator.storage.getDirectory();
const h = await root.getFileHandle(name, { create: true });
const w = await h.createWritable();
await w.write(await file.arrayBuffer());
await w.close();
}
}
}

发表评论 取消回复