electron客户端预览doc、docx、excel、pdf、ppt、csv、txt等文件类型

需求:已知文件路径,通过点击列表页的操作按钮可查看文件内容。

提供思路:实现方式有两种:

(1)打开弹窗或客户端新开窗口。不同类型应用不同插件进行直接预览查看。

(2)通过electron的shell.openPath指定系统默认程序直接打开页面。(这种方式实现简单,样式规范,具有统一性)

这里介绍第二种实现方式:

主进程监听:main.js

javascript 复制代码
const { shell } = require('electron')
// 通过系统打开窗口预览文件 
ipcMain.on('open-file', (event, filePath) => {
  // 打开文件
  shell
    .openPath(filePath)
    .then(() => {
      console.log('文件已使用系统默认程序打开')
    })
    .catch(err => {
      console.error('打开文件失败:', err)
    })
})

渲染进程preload.js

javascript 复制代码
  // 打开窗口预览文件
  openFileSend: (...args) => ipcRenderer.send("open-file", ...args),

vue组件使用:

javascript 复制代码
function previewFile (row) {
  // const filePath = 'D:\\work\\test.csv' 调试用
  window.electronAPI?.openFileSend(row.cached_file_path)
}
相关推荐
刻BITTER4 小时前
用EXCEL 将单色屏幕的Bitmap 字模数据还原回图形
单片机·嵌入式硬件·excel·arduino
匿者 衍4 小时前
POI读取 excel 嵌入式图片(支持wps 和 office)
java·excel
天外天-亮4 小时前
Vue + excel下载 + 水印
前端·vue.js·excel
不爱吃糖的程序媛5 小时前
鸿蒙PC Electron 打印服务实现详解
华为·electron·harmonyos
一千柯橘9 小时前
Electron 第一步
前端·electron
1024小神9 小时前
Electron实现多tab页案例,BrowserView/iframe/webview不同方式的区别
前端·javascript·electron
时间不说谎9 小时前
linux上编译electron源码
electron
国服第二切图仔9 小时前
Electron for 鸿蒙PC实战案例Gitcode口袋工具之HTTP请求封装的技术实现与设计解析
http·electron·gitcode
诸神缄默不语13 小时前
如何用Python处理文件:Word导出PDF & 如何用Python从Word中提取数据:以处理简历为例
python·pdf·word
i***665013 小时前
SpringBoot实战(三十二)集成 ofdrw,实现 PDF 和 OFD 的转换、SM2 签署OFD
spring boot·后端·pdf