使用window自带的showSaveFilePicker方法
const opts = {
types: [
{
accept: {
'application/vnd.ms-excel': ['.xls'],
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': [
'.xlsx'
]
}
}
]
};
const fileHandle = await window.showSaveFilePicker(opts);
const writable = await fileHandle.createWritable({ keepExistingData: true }); // 创建可写入的文件对象
// 在这里写入文件内容
const text = new Blob(['你好,世界'], { type: 'text/plain;charset=UTF-8' });
await writable.write(text);
await writable.close();
具体参考MDN:https://developer.mozilla.org/zh-CN/docs/Web/API/Window/showSaveFilePicker
文件格式参考:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types