我一直在尝试执行系统中的任何文件并在系统的默认应用程序中打开它。
例如,我有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');
});
关于如何通过node js在默认应用程序中打开系统文件的任何建议
使用exec from shelljs
示例如下:
var shell = require('shelljs');
shell.exec("D://yourdocument.pdf", function (err, out, code)) {
if(err instanceof Error)
throw err;
});
我希望它有帮助。谢谢。
在windows中,您可以简单地给出文件路径。但是在linux或mac操作系统中,你需要在前加上xdg-open来打开默认应用程序。
Linux Code &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 */ });