之前做项目的时候,遇到了一个需求:希望点击下载文件时可以选择保存文件路径,但是不希望手动修改浏览器设置。于是,各种百度之后。。。
//导出另存===== 打开目录,保存文件
const suggestedName = `文件名称`
const opts = {
suggestedName,
types: [
{
description: 'Xlsx',
accept: {
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': ['.xlsx'],
'application/vnd.ms-excel': ['.xls']
}
}
]
}
const handle = await window.showSaveFilePicker(opts)
const writable = await handle.createWritable()
await writable.write(res)
await writable.close()
仅支持http://localhost/或者https://
其他连接下,会报错,官方也有提醒