从node.js启动外部应用程序 [英] Launch an external application from node.js
问题描述
我正在编写一个使用node.js访问本地文件系统的桌面Web应用程序.我目前可以使用node.js打开文件并将其复制到硬盘驱动器上的不同位置.我还想做的是允许用户使用与文件类型关联的应用程序打开特定文件.换句话说,如果用户在Windows环境中选择"myfile.doc",它将使用该文件启动MSWord.
I'm writing a desktop web app that uses node.js to access the local file system. I can currently use node.js to open and copy files to different places on the hard drive. What I would also like to do is allow the user to open a specific file using the application that is associated with the file type. In other words, if the user selects "myfile.doc" in a Windows environment, it will launch MSWord with that file.
我一定是术语的受害者,因为除了与node.js进行通信的子进程的生成之外,我什么都找不到.我只想启动一个文件供用户查看,然后让他们决定如何处理该文件.
I must be a victim of terminology, because I haven't been able to find anything but the spawning of child processes that communicate with node.js. I just want to launch a file for the user to view and then let them decided what to do with it.
谢谢
推荐答案
您可以这样做
var cp = require("child_process");
cp.exec("document.docx"); // notice this without a callback..
process.exit(0); // exit this nodejs process
出于安全考虑,请确保该命令未显示任何错误或任何不希望的输出
it not safe thought, to ensure that the command show no errors or any undesired output
您应该添加回调参数
child_process.exec(cmd,function(error,stdout,stderr){})
you should add the callback parameter
child_process.exec(cmd,function(error,stdout,stderr){})
接下来,您可以处理事件,这样就不会阻止脚本的执行,甚至不会使用外部的node.js脚本来启动和处理从主"脚本生成的进程的输出.
and next you can work with events so you won't block execution of script or even make use of a external node.js script that launches and handles outputs from processes which you spawn from a "master" script.
这篇关于从node.js启动外部应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!