問題描述
我對電子很陌生.誰能建議我如何使用電子獲取本地文件夾的相對路徑?JavaScript 沒有這種能力.
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.
我有一個選擇文件按鈕(見快照),所以我的問題是當我選擇一個文件夾并單擊打開按鈕時,它應(yīng)該返回一個完整的目錄路徑.
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.
推薦答案
正如@phuongle 在評論中指出的,你想使用 showOpenDialog()
.像這樣的:
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']
});
更新:如果上述方法不適用于您當前的 Electron 版本,您應(yīng)該嘗試更現(xiàn)代的導入:
UPDATE: If the above isn't working for your current Electron version, you should try more modern importing:
const {dialog} = require('electron').remote;
另外,為了使用remote
,你需要在主進程中創(chuàng)建窗口時設(shè)置enableRemoteModule
:
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
}
});
這篇關(guān)于如何使用電子獲取文件夾路徑的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!