Nodejs打开PPT,DOC或任何其他文件
我一直在尝试执行系统中的任何文件,并在系统的默认应用程序中打开它。
例如,我有abc.doc文件,我想在Microsoft Word中打开它。
该代码在默认浏览器中打开URL。
var open = require('open'); open('http://www.google.com', function (err) { if (err) throw err; console.log('The user closed the browser'); });
任何build议如何通过节点js在默认应用程序中打开系统文件。
在Windows中,你可以简单地给文件path。 但是在Linux或Mac OS中,您还需要在xdg-open前缀以在默认应用程序中打开。
Linux代码和Mac OS
shell.exec("xdg-open /home/file.extension", function (err, out, code)) { /* your statements */ });
Windows代码
shell.exec("C:/file.extension", function (err, out, code)) { /* your statements */ });
使用shelljs中的exec
示例如下:
var shell = require('shelljs'); shell.exec("D://yourdocument.pdf", function (err, out, code)) { if(err instanceof Error) throw err; });
我希望它有帮助。 谢谢。