Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get folder path using electron

Tags:

I am very new to the electron. Can anyone suggest me how to get a local folder's relative path using the electron? JavaScript does not have that capability.

enter image description here

I have a Choose File button(see snapshot), so my question is that when I select a folder and click on the open button then it should return a whole directory path.

like image 207
Surjeet Bhadauriya Avatar asked Mar 22 '16 11:03

Surjeet Bhadauriya


People also ask

How do I find the file path of an electron?

getElementById('button-created-id'); buttonCreated. addEventListener('click', function (event) { ipc. send('open-file-dialog-for-file') }); Then in the main process you use the showOpenDialog to choose a file and then send the full path back to the renderer process.


2 Answers

As @phuongle pointed out in the comments you want to use showOpenDialog(). Something like this:

var remote = require('remote'); var dialog = remote.require('electron').dialog;  var path = dialog.showOpenDialog({     properties: ['openDirectory'] }); 

UPDATE: If the above isn't working for your current Electron version, you should try more modern importing:

const {dialog} = require('electron').remote; 

In addition, in order to use remote, you need to set enableRemoteModule when creating your window in your main process:

const myWindow = new BrowserWindow({     webPreferences: {         enableRemoteModule: true     } }); 
like image 102
Teak Avatar answered Sep 19 '22 03:09

Teak


In Electron we can select the directory by specifying simple input element with type="file" and webkitdirectory attribute'. <input id="myFile" type="file" webkitdirectory /> and we can get the directory full path with the path property of File object document.getElementById("myFile").files[0].path

like image 22
rajesh kumar Avatar answered Sep 19 '22 03:09

rajesh kumar