概述选择器类型过滤配合二次校验可提升导入安全。选择与校验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();

}

}

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部