11、electron showSaveDialog保存文件
一、知识点
dialog.showSaveDialog([browserWindow, ]options[, callback])
browserWindow
BrowserWindow (可选)options
Objecttitle
StringdefaultPath
Stringfilters
Array
callback
Function (可选)
成功使用这个方法的话,就返回一个可供用户选择的文件路径数组,失败返回 undefined
.
filters
指定展示一个文件类型数组, 例子 dialog.showOpenDialog
.
如果 callback
被调用, 将异步调用 API ,并且结果将用过 callback(filenames)
展示
二、示例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <input type="button" id="btn" value="保存文件" /> <script> const remote = require('@electron/remote'); const dialog = remote.dialog; const fs =require('fs'); let btn = document.getElementById("btn"); btn.onclick = (e)=>{ dialog.showSaveDialog({ title:'保存文件', defaultPath:'1.txt', filters:[{name:'txt',extensions:['txt']}] }).then(result=>{ console.log(result); fs.writeFileSync(result.filePath,"123456"); }).catch(err=>{ }); } </script> </body> </html>
三、运行效果