Home > Mobile >  NodeJS open local file using the default preview
NodeJS open local file using the default preview

Time:05-06

I am trying to create a desktop app using electron which includes a function where a user can open a desired file saved in the local storage. Sine I am using MacOS, I want to use nodejs to be able to open the file (e.g. PDF doc) in the default preview software. is there any way to do this?

Thank you in advance.

CodePudding user response:

if anyone is wondering I found a way of using

const { exec } = require('child_process')    
exec('open ~/path/to/file')

this used command shell (terminal) to open it. If there is any other better way to do it please let us know!

CodePudding user response:

A portable way is to make use of Electron's shell API, specifically the shell.openPath method:

const { shell } = require('electron');
shell.openPath("/fullpath/to/file");

It is available both in the main process and the renderer process, and can also be used to open a folder in the Finder.

  • Related