How to show an open file native dialog with Electron?

后端 未结 2 1149
眼角桃花
眼角桃花 2021-01-12 00:30

I am trying to add functionality to my Electron app that will allow users to open a file in the app, specifically plain text files. After looking at the Electron documentati

2条回答
  •  走了就别回头了
    2021-01-12 01:09

    On the main process you can use

    const {dialog} = require('electron');
    
    dialog.showOpenDialog({properties: ['openFile'] }).then(function (response) {
        if (!response.canceled) {
            // handle fully qualified file name
          console.log(response.filePaths[0]);
        } else {
          console.log("no file selected");
        }
    });
    

    response looks like:

    {
     canceled: false,
     filePaths: [
        '/'
     ]
    }
    

提交回复
热议问题