38

我有一个正在运行的 Electron 应用程序,到目前为止运行良好。对于上下文,我需要运行/打开一个外部文件,该文件是一个 Go-lang 二进制文件,它将执行一些后台任务。基本上,它将充当后端并公开 Electron 应用程序将使用的 API。

到目前为止,这是我进入的:

  • 我尝试使用child_process以“节点方式”打开文件,但可能由于路径问题,我无法打开示例 txt 文件。

  • Electron API 公开了一个打开文件事件,但它缺乏文档/示例,我不知道它是否有用。

而已。如何在 Electron 中打开外部文件?

4

5 回答 5

72

您可能想研究几个 api,看看哪些对您有帮助。

fs

fs模块允许您直接打开文件进行读写。

var fs = require('fs');
fs.readFile(p, 'utf8', function (err, data) {
  if (err) return console.log(err);
  // data is the contents of the text file we just read
});

小路

path模块允许您以与平台无关的方式构建和解析路径。

var path = require('path');
var p = path.join(__dirname, '..', 'game.config');

shellapi 是一个纯电子 api,您可以使用它在给定路径上执行文件,这将使用操作系统默认应用程序打开文件。

const {shell} = require('electron');
// Open a local file in the default app
shell.openItem('c:\\example.txt');

// Open a URL in the default way
shell.openExternal('https://github.com');

子进程

假设您的 golang 二进制文件是可执行文件,那么您将使用child_process.spawn它来调用它并与之通信。这是一个节点 API。

var path = require('path');
var spawn = require('child_process').spawn;

var child = spawn(path.join(__dirname, '..', 'mygoap.exe'), ['game.config', '--debug']);
// attach events, etc.

添加在

如果您的 golang 二进制文件不是可执行文件,那么您将需要制作一个本机插件包装器。

于 2015-10-29T22:18:41.053 回答
0

也许你正在寻找这个?

dialog.showOpenDialog 参考:https ://www.electronjs.org/docs/api/dialog

如果使用 electron@13.1.0,你可以这样做:

const { dialog } = require('electron')
console.log(dialog.showOpenDialog({ properties: ['openFile', 'multiSelections'] }))

dialog.showOpenDialog(function(file_paths){
  console.info(file_paths)  // => this gives the absolute path of selected files.
})

当上面的代码被触发时,你可以看到一个像这样的“打开文件对话框”(win/mac/linux 的不同视图样式)

在此处输入图像描述

于 2021-06-06T00:49:32.400 回答
0

Electron 允许使用nodejs 包

换句话说,像在 node 中一样导入 node 包,例如:

var fs = require('fs');

要运行 golang 二进制文件,您可以使用child_process模块。文档很详尽。

编辑:您必须解决路径差异。该open-file事件是一个客户端事件,由窗口触发。不是你想要的。

于 2015-10-15T17:16:31.033 回答
0

我也完全在这个问题上苦苦挣扎,几乎七年后,文档还不清楚 Linux 的情况。

因此,在 Linux 上它在这方面属于 Windows 处理,这意味着您必须process.argv在主处理器中查看全局,数组中的第一个值是触发应用程序的路径。第二个参数(如果存在)保存请求打开应用程序的路径。例如,这是我的测试用例的输出:

Array(2)
0: "/opt/Blueprint/b-test"
1: "/home/husayngonzalez/2022-01-20.md"
length: 2

因此,当您创建一个新窗口时,您检查长度,process.argv如果它大于 1,即 = 2,这意味着您有一个请求使用您的应用程序打开的路径。

假设您将应用程序打包为能够处理这些文件,并且您将操作系统设置为请求您的应用程序打开这些文件。

于 2022-01-20T21:23:46.487 回答
-1

我知道这并不完全符合您的规范,但它确实将您的 golang 二进制文件和 Electron 应用程序完全分开。

我这样做的方法是将 golang 二进制文件公开为 Web 服务。像这样

package main

import (
    "fmt"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    //TODO: put your call here instead of the Fprintf
    fmt.Fprintf(w, "HI there from Go Web Svc. %s", r.URL.Path[1:])
}

func main() {
    http.HandleFunc("/api/someMethod", handler)
    http.ListenAndServe(":8080", nil)
}

然后从 Electron 只需使用 javascript 函数对 Web 服务进行 ajax 调用。像这样(你可以使用 jQuery,但我发现这个纯 js 工作正常)

function get(url, responseType) {
    return new Promise(function(resolve, reject) {
      var request = new XMLHttpRequest();
      request.open('GET', url);
      request.responseType = responseType;

    request.onload = function() {
    if (request.status == 200) {
      resolve(request.response);
    } else {
      reject(Error(request.statusText));
    }
  };

  request.onerror = function() {
    reject(Error("Network Error"));
  };

  request.send();
});

使用这种方法,您可以执行类似的操作

get('localhost/api/somemethod', 'text')
  .then(function(x){
    console.log(x);
  }
于 2015-05-22T03:19:45.780 回答