问题描述
我有一个正在运行的Electron应用程序,到目前为止运行良好。对于上下文,我需要运行/打开一个外部文件,该文件是Go-lang二进制文件,它将执行一些后台任务。
基本上,它将充当后端并公开Electron应用程序将使用的API。
I have a running Electron app and is working great so far. For context, I need to run/open a external file which is a Go-lang binary that will do some background tasks.Basically it will act as a backend and exposing an API that the Electron app will consume.
到目前为止,这就是我要学习的内容:
So far this is what i get into:
-
我尝试使用,但由于路径问题,我无法打开示例txt文件。
I tried to open the file with the "node way" using child_process but i have fail opening the a sample txt file probably due to path issues.
Electron API公开了事件,但缺少文档/示例,我不知道它是否有用。
The Electron API expose a open-file event but it lacks of documentation/example and i don't know if it could be useful.
就是这样。
我如何在Electron中打开外部文件?
That's it.How i open an external file in Electron ?
推荐答案
有一些api您可能想学习
There are a couple api's you may want to study up on and see which helps you.
模块可让您打开文件以直接进行读写。
The fs
module allows you to open files for reading and writing directly.
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
The path
module allows you to build and parse paths in a platform agnostic way.
var path = require('path');
var p = path.join(__dirname, '..', 'game.config');
shell
api是电子
shell
The shell
api is an electron only api that you can use to shell execute a file at a given path, which will use the OS default application to open the file.
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');
child_process
假设您的golang二进制文件是可执行文件,那么您将使用调用它并与之通信。这是一个节点api。
child_process
Assuming that your golang binary is an executable then you would use child_process.spawn
to call it and communicate with it. This is a node 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二进制文件不是可执行文件,则您需要制作一个包装器。
这篇关于用Electron打开外部文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!