打开文件选择对话框可以使用dialog.showOpenDialog()
方法来打开,它有两个参数,一个是设置基本属性,另一个是回调函数,如果是异步可以使用then
来实现。
-
title : String (可选),对话框的标题
-
defaultPath : String (可选),默认打开的路径
-
buttonLabel : String (可选), 确认按钮的自定义标签,当为空时,将使用默认标签
-
filters : 文件选择过滤器,定义后可以对文件扩展名进行筛选
-
properties:打开文件的属性,比如打开文件还是打开文件夹,甚至是隐藏文件
1 新建demo4.html
<!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>
<button id='openBtn' type="">打开图片</button>
<img id='images' style="width: 100%;"/>
<script>
const {dialog} = require('@electron/remote')
var openBtn=document.getElementById('openBtn')
openBtn.οnclick=()=>{
dialog.showOpenDialog({
title:'请选择你喜欢的小姐姐照片',
defaultPath:'xiaojiejie.jpeg',
filters:[{
name:'img',
extensions:['jpeg']
}],
buttonLabel:'打开小姐姐'
}).then(result=>{
let images=document.getElementById('images')
images.setAttribute('src',result.filePaths[0])
}).catch(err=>{
console.log(err)
})
}
</script>
</body>
</html>
2 修改main.js 讲主进程指向demo4.html
3 运行程序