Javascript 如何在electron中使用filedialog?

Javascript 如何在electron中使用filedialog?,javascript,html,electron,Javascript,Html,Electron,我是electron的新手,正在尝试打开一个文件对话框,允许用户使用以下代码选择特定文件: const {remote} = require("remote"); const {dialog} = require('electron').remote; function openFileDialog() { const savePath = dialog.showSaveDialog(); console.log(savePath) } <input

我是electron的新手,正在尝试打开一个文件对话框,允许用户使用以下代码选择特定文件:

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

function openFileDialog() {
    const savePath = dialog.showSaveDialog();
    console.log(savePath)
}
<input class="btn btn-dark" type="button" value="Input" onclick="openFileDialog();">
   <script src="./../javascript/settings.js"></script>
window.onload=function(){
    document.getElementById("dialogBtn").addEventListener("click", openFileDialog);
}
但是,当我尝试此操作时,控制台中会出现一个错误,提示:

未捕获的TypeError:无法读取的属性“showSaveDialog” 未定义

有人知道我做错了什么吗

编辑: 我现在使用这段代码,正如下面建议的那样:

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

function openFileDialog() {
    const savePath = dialog.showSaveDialog(null);
    console.log(savePath)
}
在名为settings.js的文件中,我使用以下代码调用该文件:

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

function openFileDialog() {
    const savePath = dialog.showSaveDialog();
    console.log(savePath)
}
<input class="btn btn-dark" type="button" value="Input" onclick="openFileDialog();">
   <script src="./../javascript/settings.js"></script>
window.onload=function(){
    document.getElementById("dialogBtn").addEventListener("click", openFileDialog);
}

我使用以下代码导入脚本:

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

function openFileDialog() {
    const savePath = dialog.showSaveDialog();
    console.log(savePath)
}
<input class="btn btn-dark" type="button" value="Input" onclick="openFileDialog();">
   <script src="./../javascript/settings.js"></script>
window.onload=function(){
    document.getElementById("dialogBtn").addEventListener("click", openFileDialog);
}


我试过带遥控器和不带遥控器。我仍然收到相同的错误

如果您在
主进程中使用它,如果您想从
渲染器
进程中使用它,这应该可以工作:

const { dialog } = require('electron').remote;
另外,最好在脚本中定义
事件处理程序
。以下是一个功能示例:

<input class="btn btn-dark" type="button" value="Input" id="dialogBtn">

我只是用这些代码解决了这个问题。第一个问题是,我需要启用remoteModule
enableMoteModule:true,
,然后我需要使用以下代码等待DOM加载:

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

function openFileDialog() {
    const savePath = dialog.showSaveDialog();
    console.log(savePath)
}
<input class="btn btn-dark" type="button" value="Input" onclick="openFileDialog();">
   <script src="./../javascript/settings.js"></script>
window.onload=function(){
    document.getElementById("dialogBtn").addEventListener("click", openFileDialog);
}

谢谢巴达维少校的帮助

您在哪里定义此函数?如果您在渲染器进程中运行此代码,首先必须导入
remote
,然后才导入
const{dialog}=remote我已更新我的问题我已更新我的问题。从渲染过程中使用它意味着什么?@Victorgunnarson
main
过程是文件
main.js
,例如,在该文件中创建了
BrowserWindow
。而
呈现程序
流程就是您在UI脚本中使用的流程,例如,它似乎就是您的情况。但是,我注意到您以错误的方式导入
对话框
,请尝试使用上述解决方案。我在本地运行了上述解决方案并正常打开了对话框谢谢您的帮助和耐心,我刚刚粘贴了您的代码。但是,我现在得到一个错误:uncaughttypeerror:无法分解“require(…).remote”的属性“dialog”,因为它是未定义的。