問題描述
我將 NodeJS/Electron 用于桌面應用程序.
I'm using NodeJS/Electron for a desktop app.
我想做的是用它的操作系統的默認應用程序打開一個文件,比如用 Word 的 .docx.
What I wanna do, is to open a file with it's OS' default application, like .docx with Word.
到目前為止,我嘗試的是使用 child_process.spawn、.exec 或 .execFile 的方法,但我什么也沒得到.
What I tried so far are approaches using child_process.spawn, .exec or .execFile but I don't get anything.
這是我的實際代碼:
var fs = require('fs'),
cp = require('child_process');
cp.spawn(__dirname + '/test.docx');
提前致謝.
推薦答案
使用openItem()
函數由 Electron 的 shell
模塊提供,例如:
Use the openItem()
function provided by Electron's shell
module, for example:
const shell = require('electron').shell;
const path = require('path');
shell.openItem(path.join(__dirname, 'test.docx'));
根據文檔,shell
模塊應該在主/瀏覽器和渲染器進程中都可用.
According to the docs the shell
module should be available in both the main/browser and renderer processes.
注意:Electron 9.0.0 shell.openItem
API 已替換為異步 shell.openPath
API.shell.openPath 文檔
Note: Electron 9.0.0 The
shell.openItem
API has been replaced with an asynchronousshell.openPath
API. shell.openPath docs
這篇關于使用 NodeJS 和 Electron 使用操作系統的默認應用程序(帶有 Word 的 docx 等)打開外部文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!