首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >选定目录的电子JS -获取路径

选定目录的电子JS -获取路径
EN

Stack Overflow用户
提问于 2020-11-25 10:33:49
回答 1查看 427关注 0票数 0

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

我正在使用ipc,看起来有些代码可以工作,但是看起来我无法让mainIpc将路径发送回渲染器。

希望蜂箱能帮上忙,谢谢!

渲染者:

代码语言:javascript
复制
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);
});

代码语言:javascript
复制
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");
        }
    );
}
});

编辑:添加安装程序安装程序

代码语言:javascript
复制
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();
}
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-25 19:48:15

我得到了一些帮助。所以如果有人需要同样的程序,我会尽力解释我要做什么。

所以,总的来说,我必须添加一个“然后”,因为showDialog返回一个承诺

代码语言:javascript
复制
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);
        });
}

});

这将返回一个数组,其中路径位于

在呈现器中,我忘记添加事件作为参数。

代码语言:javascript
复制
ipc.on("path:selected", (event, path) => {
  chosenPath = path;
  console.log("Full path: ", chosenPath[0]);
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65002896

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档