概述FSA 的 `createWritable` 提供可写流能力。本文展示分块写入、进度回调与取消控制。分块写入与取消const supportsFSA = typeof window.showSaveFilePicker === 'function';

async function saveStream(name, file, size = 1024 * 1024, onProgress, signal) {

if (!supportsFSA) throw new Error('FSA unsupported');

const handle = await window.showSaveFilePicker({ suggestedName: name });

const ws = await handle.createWritable();

let o = 0, sent = 0;

while (o < file.size) {

if (signal && signal.aborted) { await ws.close(); throw new DOMException('aborted','AbortError'); }

const b = file.slice(o, o + size);

const ab = await b.arrayBuffer();

await ws.write(new Uint8Array(ab));

o += size; sent += b.size; onProgress && onProgress(sent);

}

await ws.close();

}

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部