亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Electron JS - 獲取所選目錄的路徑

Electron JS - 獲取所選目錄的路徑

慕雪6442864 2023-06-15 16:00:19
我在編程世界相當陌生。我正在制作一個應用程序,應該可以在其中選擇一個目錄,用于保存一些生成的文件。我正在使用 ipc,看起來有些代碼可以工作,但看起來我無法讓 mainIpc 將路徑發送回渲染器。希望蜂巢能幫上忙,先謝謝了!渲染器: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);});主要的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");        }    );}});
查看完整描述

1 回答

?
揚帆大魚

TA貢獻1799條經驗 獲得超9個贊

我在某種幫助下弄明白了。因此,如果有人需要相同的程序,我會嘗試解釋我做了什么。


所以,總的來說,我不得不添加一個 then,因為 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]);

});


查看完整回答
反對 回復 2023-06-15
  • 1 回答
  • 0 關注
  • 469 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號