前端学习之路Electron——选择文件对话框

打开选择文件对话框——dialog

这个title就是下面红色框你的title

		const {dialog} = require('electron').remote;
        const {dialog} = require('electron').remote
        let opentext = document.querySelector('#opentext');
        opentext.onclick = function(e){
            dialog.showOpenDialog({
                title:'请选择你的文件',
            })
        }

在这里插入图片描述

默认打开的路径

        opentext.onclick = function(e){
            dialog.showOpenDialog({
                title:'请选择你的文件',
                defaultPath:'',//默认打开的文件路径选择
            })
        }

过滤文件格式

        opentext.onclick = function(e){
            dialog.showOpenDialog({
                title:'请选择你的文件',
                defaultPath:'',//默认打开的文件路径选择
                filters:[{ //过滤掉你不需要的文件格式
                    name:'image',
                    extensions:['jpg','png']
                }]
            })
        }

修改打开提交按钮

        opentext.onclick = function (e) {
            dialog.showOpenDialog({
                title: '请选择你的文件',
                defaultPath: '', //默认打开的文件路径选择
                filters: [{ //过滤掉你不需要的文件格式
                    name: 'image',
                    extensions: ['jpg', 'png']
                }],
                buttonLabel: '发送表情包'
            })
        }

在这里插入图片描述

将选择的图片放到窗口上

        opentext.onclick = function(e){
            dialog.showOpenDialog({
                title:'请选择你的文件',
                defaultPath:'',//默认打开的文件路径选择
                filters:[{ //过滤掉你不需要的文件格式
                    name:'image',
                    extensions:['jpg','png']
                }],
                buttonLabel:'发送表情包'
            }).then(res=>{
                let openimg = document.querySelector('#openimg');
                openimg.setAttribute('src',res.filePaths[0])
            }).catch(req=>{
                console.log(req)
            })
        }

在这里插入图片描述

全部

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- <button id="btn">
        打开新的窗口
    </button>
    <div>

    </div> -->
    <a id="aHref" href="https://www.baidu.com/">打开外部链接</a>
    <div id="myweb">
        open myweb
    </div>
    <div id='fatview'>

    </div>
    <div id="opentext">
        opentext
    </div>
    <img src="" alt="" id="openimg" style="width:100%">
    <script src="./render/openweb.js"></script>
    <script>
        const {dialog} = require('electron').remote
        let opentext = document.querySelector('#opentext');
        opentext.onclick = function(e){
            dialog.showOpenDialog({
                title:'请选择你的文件',
                defaultPath:'',//默认打开的文件路径选择
                filters:[{ //过滤掉你不需要的文件格式
                    name:'image',
                    extensions:['jpg','png']
                }],
                buttonLabel:'发送表情包'
            }).then(res=>{
                let openimg = document.querySelector('#openimg');
                openimg.setAttribute('src',res.filePaths[0])
            }).catch(req=>{
                console.log(req)
            })
        }
    </script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值