最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Electron - Open Folder Dialog - Stack Overflow

programmeradmin6浏览0评论

I want the user to be able to pick a folder from the folder dialog box.
So far, I've tried following this tutorial unsuccessfully.
I got stuck on the part of

exports.selectDirectory = function () {
  // dialog.showOpenDialog as before
}

What do I need to do in order to retrieve the full path of the selected folder?
Thanks!

I want the user to be able to pick a folder from the folder dialog box.
So far, I've tried following this tutorial unsuccessfully.
I got stuck on the part of

exports.selectDirectory = function () {
  // dialog.showOpenDialog as before
}

What do I need to do in order to retrieve the full path of the selected folder?
Thanks!

Share Improve this question asked Sep 3, 2017 at 19:49 AviAvi 2,1708 gold badges26 silver badges43 bronze badges 1
  • Well, I solved the problem by using both the article post that I mentioned in the main post and this answer. – Avi Commented Sep 5, 2017 at 18:09
Add a ment  | 

2 Answers 2

Reset to default 11

Dialog api is available in main process(https://electron.atom.io/docs/).

To create a dialog box you will have to tell your main process to do so by sending a message from renderer process.

Try this code:

// in your renderer process:-

const ipcRenderer = require('electron').ipcRenderer;

ipcRenderer.send('selectDirectory');


//in you main process:-

const electron = require('electron');

const ipcMain = electron.ipcMain;

const dialog = electron.dialog;

//hold the array of directory paths selected by user

let dir;

ipcMain.on('selectDirectory', function() {

    dir = dialog.showOpenDialog(mainWindow, {

        properties: ['openDirectory']

    });

});

Note: mainWindow here, it's the parent browserWindow which will hold the dialog box.

You need to use electron remote

const {dialog} = require('electron'),
WIN = new BrowserWindow({width: 800, height: 600})

/*
//renderer.js - a renderer process
const {remote} = require('electron'),
dialog = remote.dialog,
WIN = remote.getCurrentWindow();
*/

let options = {
 // See place holder 1 in above image
 title : "Custom title bar", 

 // See place holder 2 in above image
 defaultPath : "D:\\electron-app",

 // See place holder 3 in above image
 buttonLabel : "Custom button",

 // See place holder 4 in above image
 filters :[
  {name: 'Images', extensions: ['jpg', 'png', 'gif']},
  {name: 'Movies', extensions: ['mkv', 'avi', 'mp4']},
  {name: 'Custom File Type', extensions: ['as']},
  {name: 'All Files', extensions: ['*']}
 ],
 properties: ['openFile','multiSelections']
}

//Synchronous
let filePaths = dialog.showOpenDialog(WIN, options)
console.log('filePaths)

//Or asynchronous - using callback
dialog.showOpenDialog(WIN, options, (filePaths) => {
 console.log(filePaths)
})
发布评论

评论列表(0)

  1. 暂无评论