Nodejs使用默认预览打开本地文件

发布于 2025-01-26 14:36:03 字数 139 浏览 4 评论 0原文

我正在尝试使用Electron创建一个桌面应用,该应用程序包含一个函数,其中用户可以打开保存在本地存储中的所需文件。正弦我正在使用MacOS,我想使用nodejs能够在默认预览软件中打开文件(例如PDF DOC)。有什么办法做到这一点吗?

先感谢您。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘虞初梦 2025-02-02 14:36:03

一种便携的方法是利用电子的 shell> shell api,特别是<<< rel =“ nofollow noreferrer”> shell.openpath.opentath

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

a href =“ https://www.electronjs.org/docs/latest/latest/api/shell#shellpopenpath ” 渲染器过程,也可以用来打开查找器中的文件夹。

A portable way is to make use of the 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 in the renderer process, and it can also be used to open a folder in the Finder.

z祗昰~ 2025-02-02 14:36:03

方法

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

如果有人想知道我找到了一种使用此二手命令壳(终端)打开它的 。如果还有其他更好的方法可以做到这一点,请告诉我们!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文