电子dialog.showOpenDialog()返回未定义

时间:2019-12-10 18:58:03

标签: javascript html css electron

const { app, BrowserWindow, dialog } = require('electron');

const fs = require('fs');

const path = require('path')

let mainWindow = null;

app.on('ready', () => {
    mainWindow = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } });

    mainWindow.loadFile(path.join(__dirname, 'index.html'));
    mainWindow.once('ready-to-show', () => {
        mainWindow.show();
        //mainWindow.webContents.openDevTools();

        getFileFromUser();
    });

    mainWindow.on('closed', () => {
        mainWindow = null;
    });
});

const getFileFromUser = async () => {
    // Triggers the OS' Open File Dialog box. We also pass it as a Javascript
    // object of different configuration arguments to the function

    const files = await dialog.showOpenDialog(mainWindow, {
        // The Configuration object sets different properties on the Open File Dialog 
        properties: ['openFile']
    }).catch((err)=> {
        return callback(err)
    })

    if (!files) {
        return;
    }

    const file = files[0];
    const content = fs.readFileSync(file).toString();
    console.log(content)
}

0 个答案:

没有答案