electron 调用本地文件对话框

使用electron 调用本地的文件对话框。详细说明,可以参考https://www.electronjs.org/docs/api/dialog#dialogshowsavedialogbrowserwindow-options

我这里提供我的实现,亲测可用。

打开文件

 open_file() {
        const { dialog } = require('electron').remote;
        dialog.showOpenDialog({
            title: "打开文件",
            defaultPath: "",
            properties: ['openFile', 'multiSelections'],
            filters: [
                { name: 'Text', extensions: ['html', 'js', 'json', 'md'] },
                { name: 'Custom File Type', extensions: ['as'] },
                { name: 'All Files', extensions: ['*'] }
            ]
        }).then(result => {
            console.log(result.filePaths);
            const fs = require('fs');

            // _this.fs.readFile(result.filePaths[0], 'utf-8', (err, data) => {
            //     if (err) {
            //         alert("An error ocurred reading the file :" + err.message)
            //         return
            //     }
            //     // handle the file content
            //     event.sender.send('fileData', data)
            // })
            return result.filePaths[0];

        }).catch(err => {
            console.log(err)
        })
    }

保存文件

save_file(info) {
        var _this = this;
        const { dialog } = require('electron').remote;
        dialog.showSaveDialog({
            title: "保存文件",
            defaultPath: "",
            properties: ['saveFile'],
            filters: [
                { name: 'Text', extensions: ['json'] },
                { name: 'Custom File Type', extensions: ['as'] },
                { name: 'All Files', extensions: ['*'] }
            ]
        }).then(result => {
            const fs = require('fs');

            var fd = fs.openSync(result.filePath, 'w');
            fs.writeSync(fd, info);
            fs.closeSync(fd);
        }).catch(err => {
            console.log(err)
        })
    }

信息提示框

/**
     * 
     * @param {_title} _title : 消息框抬头
     * @param {_type} _type : 消息类型,比如error,none, info, warnning等
     * @param {_msg} _msg: 消息内容 
     */
    show_message(_title, _type, _msg) {
        const { dialog } = require('electron').remote;

        if (_title == "" || _title == null || _title == undefined) _title = "错误";
        if (_type == "" || _type == null || _type == undefined) _type = "error";
        if (_msg == "" || _msg == null || _msg == undefined) _msg = "This is a message.";
        dialog.showMessageBox({
            title: _title,
            type: _type,
            message: _msg
        })
    }

最后附上一张截图

整个文件操作实现,使用了ES6中的class特性。完整代码如下

/**
 * @description: 集成node模块,调用electron 使用系统资源
 * @author: zy
 * @field: 2020.2.20
 * @param: 具体参数可以参考https://cloud.tencent.com/developer/section/1116149
 */

class Dialog_Mgr {
    constructor() {
        var _this = this;
        _this.fs = require('fs');
    }

    open_file() {
        const { dialog } = require('electron').remote;
        dialog.showOpenDialog({
            title: "打开文件",
            defaultPath: "",
            properties: ['openFile', 'multiSelections'],
            filters: [
                { name: 'Text', extensions: ['html', 'js', 'json', 'md'] },
                { name: 'Custom File Type', extensions: ['as'] },
                { name: 'All Files', extensions: ['*'] }
            ]
        }).then(result => {
            console.log(result.filePaths);
            const fs = require('fs');

            // _this.fs.readFile(result.filePaths[0], 'utf-8', (err, data) => {
            //     if (err) {
            //         alert("An error ocurred reading the file :" + err.message)
            //         return
            //     }
            //     // handle the file content
            //     event.sender.send('fileData', data)
            // })
            return result.filePaths[0];

        }).catch(err => {
            console.log(err)
        })
    }

    save_file(info) {
        var _this = this;
        const { dialog } = require('electron').remote;
        dialog.showSaveDialog({
            title: "保存文件",
            defaultPath: "",
            properties: ['saveFile'],
            filters: [
                { name: 'Text', extensions: ['json'] },
                { name: 'Custom File Type', extensions: ['as'] },
                { name: 'All Files', extensions: ['*'] }
            ]
        }).then(result => {
            const fs = require('fs');

            var fd = fs.openSync(result.filePath, 'w');
            fs.writeSync(fd, info);
            fs.closeSync(fd);
        }).catch(err => {
            console.log(err)
        })
    }

    /**
     * 
     * @param {_title} _title : 消息框抬头
     * @param {_type} _type : 消息类型,比如error,none, info, warnning等
     * @param {_msg} _msg: 消息内容 
     */
    show_message(_title, _type, _msg) {
        const { dialog } = require('electron').remote;

        if (_title == "" || _title == null || _title == undefined) _title = "错误";
        if (_type == "" || _type == null || _type == undefined) _type = "error";
        if (_msg == "" || _msg == null || _msg == undefined) _msg = "This is a message.";
        dialog.showMessageBox({
            title: _title,
            type: _type,
            message: _msg
        })
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老朱自强不息

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值