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.
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.
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.
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 } });
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With