概述选择器类型过滤配合二次校验可提升导入安全。选择与校验const types = [{ description: '文本', accept: { 'text/plain': ['.txt'] } }, { description: 'JSON', accept: { 'application/json': ['.json'] } }];
async function pickAndImport(base = 'import') {
const handles = await window.showOpenFilePicker({ multiple: true, types });
const root = await navigator.storage.getDirectory();
const dir = await root.getDirectoryHandle(base, { create: true });
for (const h of handles) {
const f = await h.getFile();
const ok = f.type === 'text/plain' || f.type === 'application/json';
if (!ok) continue;
const fh = await dir.getFileHandle(f.name, { create: true });
const w = await fh.createWritable();
await w.write(await f.arrayBuffer());
await w.close();
}
}

发表评论 取消回复