概述通过解析 `Content-Disposition` 获取服务器建议文件名,并完成本地保存。解析与保存function parseFilename(cd) {
if (!cd) return 'download';
const m = cd.match(/filename\*=UTF-8''([^;]+)|filename="?([^";]+)"?/i);
return decodeURIComponent(m?.[1] || m?.[2] || 'download');
}
async function saveResponse(url) {
const res = await fetch(url);
const name = parseFilename(res.headers.get('Content-Disposition'));
const h = await window.showSaveFilePicker({ suggestedName: name });
const ws = await h.createWritable();
const ab = await res.arrayBuffer();
await ws.write(new Uint8Array(ab));
await ws.close();
}

发表评论 取消回复