概述保存对话框支持类型列表与建议文件名。本文给出类型与 MIME 映射的标准用法。类型映射与保存const types = [
{
description: '文本文件',
accept: { 'text/plain': ['.txt'] }
},
{
description: 'JSON 文件',
accept: { 'application/json': ['.json'] }
}
];
async function saveText(name, text, mime = 'text/plain') {
const handle = await window.showSaveFilePicker({ suggestedName: name, types });
const ws = await handle.createWritable();
const blob = new Blob([text], { type: mime });
await ws.write(blob);
await ws.close();
}

发表评论 取消回复