Javascript 如何使用电子获取文件夹路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36152857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:42:22  来源:igfitidea点击:

How to get folder path using electron

javascriptangularjsnode.jselectron

提问by Surjeet Bhadauriya

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.

我对电子很陌生。谁能建议我如何使用电子获取本地文件夹的相对路径?JavaScript 没有这种能力。

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.

我有一个选择文件按钮(见快照),所以我的问题是,当我选择一个文件夹并单击打开按钮时,它应该返回整个目录路径。

回答by Teak

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

正如@phuongle 在您要使用的评论中指出的那样showOpenDialog()。像这样的东西:

var remote = require('remote');
var dialog = remote.require('electron').dialog;

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

UPDATE: the above isn't working for the current version, you must use ES6 imports..

更新:以上不适用于当前版本,您必须使用 ES6 导入..

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

回答by rajesh kumar

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

在 Electron 中,我们可以通过指定带有 type="file" 和 webkitdirectory 属性的简单输入元素来选择目录。 <input id="myFile" type="file" webkitdirectory />我们可以使用 File 对象的 path 属性获取目录完整路径document.getElementById("myFile").files[0].path

回答by inukshuk

You would use Node's path.relativefor that.

你会使用 Node 的path.relative

回答by Felipe N Moura

The solution for me was simply using all in lowercase, with a value trueas string in my react component. No extra configuration was required.

对我来说,解决方案只是使用全部小写,true在我的反应组件中使用一个字符串作为值。不需要额外的配置。

Like so:

像这样:

<input
    id="path-picker"
    type="file"
    webkitdirectory="true"
/>

Edit

编辑

It turns out that, as mentioned by @cbartondock, it will recursively look for files in the directory, which is not good!

事实证明,正如@cbartondock 提到的,它会递归查找目录中的文件,这并不好!

I ended up using the required electron remote's dialog.

我最终使用了所需的电子遥控器对话框。