Open a File with Default Program in Node-Webkit

open a file with default program in node-webkit

as PSkocik said, first detect the platform and get the command line :

function getCommandLine() {
switch (process.platform) {
case 'darwin' : return 'open';
case 'win32' : return 'start';
case 'win64' : return 'start';
default : return 'xdg-open';
}
}

second , execute the command line followed by the path

var exec = require('child_process').exec;

exec(getCommandLine() + ' ' + filePath);

Open external file with OS' default application (docx with Word, etc.) using NodeJS and Electron

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'));

According to the docs the shell module should be available in both the main/browser and renderer processes.

Note: Electron 9.0.0 The shell.openItem API has been replaced with an asynchronous shell.openPath API. shell.openPath docs

Is there any way to open file in its default application using JavaScript?

Short answer: no. Can you imagine the security nightmare if JavaScript was allowed to execute arbitrary binary files on a user's machine?

Your solution is going to be to find a JavaScript library that allows for handling those files, similar to how PDF.js was made for PDFs in-browser.

How can I find the correct program to open a given file type in node.js?

You could try to use START command.

var child = spawn("cmd.exe", ["/c", "start", "excelfile.xls"], {env: process.env, cwd: "c:\\"});
child.unref();

open a file with its default programme

There is no way for you to choose which application will be used to open your files with javascript...It just doesn't have that power.

Open all file types with Default OS program

I set nodeIntegration to true and added window.$ = window.jQuery = require('jquery'); to my mainWindow html. and now it recognizes shell.openItem.



Related Topics



Leave a reply



Submit