Javascript Electron JS-获取所选目录的路径

Javascript Electron JS-获取所选目录的路径,javascript,electron,Javascript,Electron,我是编程界的新手。我正在制作一个应用程序,它应该可以选择一个目录,在那里保存一些生成的文件 我正在使用ipc,看起来有些代码是有效的,但是看起来我无法让主ipc将路径发送回渲染器 我希望蜂箱能帮上忙,提前谢谢 渲染器: const electron = require("electron"); const ipc = require("electron").ipcRenderer; createBtn.addEventListener("

我是编程界的新手。我正在制作一个应用程序,它应该可以选择一个目录,在那里保存一些生成的文件

我正在使用ipc,看起来有些代码是有效的,但是看起来我无法让主ipc将路径发送回渲染器

我希望蜂箱能帮上忙,提前谢谢

渲染器:

const electron = require("electron");
const ipc = require("electron").ipcRenderer;    

createBtn.addEventListener("click", (event) => {
ipc.send("path:get");
});

ipc.on("path:selected", function (path) {
console.log("Full path: ", path);
});
Main

const ipc = require("electron").ipcMain;
const os = require("os");
const { dialog } = require("electron");

ipc.on("path:get", function (event) {
if (os.platform() === "linux" || os.platform() === "win32") {
    dialog.showOpenDialog(
        {
            properties: ["openFile"],
        },
        function (files) {
            if (files) win.webContents.send("path:selected", files[0]);
            console.log("SENT");
        }
    );
} else {
    dialog.showOpenDialog(
        {
            properties: ["openFile", "openDirectory"],
        },
        function (files) {
            if (files) win.webContents.send("path:selected", files[0]);
            console.log("SENT");
        }
    );
}
});
编辑:添加设置 设置

const { app, BrowserWindow } = require("electron");

const ipc = require("electron").ipcMain;
const os = require("os");
const { dialog } = require("electron");

try {
    require("electron-reloader")(module);
} catch (_) {}

let win;

function createWindow() {
win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
        nodeIntegration: true,
    },
});

win.loadFile("./src/index.html");
}

app.whenReady().then(createWindow);

app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
    app.quit();
}
});

app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
    createWindow();
}
});



    

我在某种帮助下找到了答案。 所以,如果有人需要同样的程序,我会尽力解释我需要做什么

因此,大体上,我必须添加一个,因为showDialog返回一个承诺

if (os.platform() === "linux" || os.platform() === "win32") {
    dialog
        .showOpenDialog({
            properties: ["openFile", "openDirectory"],
        })
        .then((result) => {
            if (result) win.webContents.send("path:selected", result.filePaths);
        })
        .catch((err) => {
            console.log(err);
        });
} else {
    dialog
        .showOpenDialog({
            properties: ["openFile", "openDirectory"],
        })
        .then((result) => {
            console.log(result.filePaths);
            if (result) win.webContents.send("path:selected", result.filePaths);
        })
        .catch((err) => {
            console.log(err);
        });
}
}))

这将发回一个路径为[0]的数组

在渲染器中,我忘记将事件添加为参数

ipc.on("path:selected", (event, path) => {
  chosenPath = path;
  console.log("Full path: ", chosenPath[0]);
});

你确定
win
在你调用它的地方可用吗?添加了我的设置。我已在全球范围内宣布
win
。所以我很确定它是可用的。