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)
}
相关推荐
木木子99999 小时前
Excel——重复值处理
excel
SEO-狼术12 小时前
Find Content Fast in PDF Documents
pdf
UrbanJazzerati16 小时前
Excel 函数 `SUBSTITUTE` 用法详解:替换文本中的字符
excel
何中应17 小时前
EasyExcel使用(二:写出)
java·后端·maven·excel
m0_4621851117 小时前
VB解除excel保护工作表
excel
qq_4639448617 小时前
如何把Excel文件导入Navicat?
数据库·excel
不太厉害的程序员18 小时前
Excel 将数据导入到SQLServer数据库
数据库·sqlserver·excel
开开心心_Every18 小时前
多线程语音识别工具
javascript·人工智能·ocr·excel·语音识别·symfony
Hi2024021719 小时前
支持OCR和AI解释的Web PDF阅读器:解决大文档阅读难题
pdf·flask·llm·ocr·阅读器
VB5941 天前
《考研 考证 各学科 各专业 思维导图汇总》PDF 5.2GB
考研·pdf