vue+electron中实现文件下载打开wps预览

下载事件

win.webContents.downloadURL(url)

触发session的will-download事件

javascript 复制代码
win.webContents.session.on('will-download', (event, downloadItem, webContents) => {
	// 设置文件保存路径
    // 如果用户没有设置保存路径,Electron将使用默认方式来确定保存路径(通常会提示保存对话框)
    item.setSavePath(savePath)
	
	item.on('updated', (event, state) => {
	// 监听下载过程, state有两种情况
	// interrupted 下载被中断,可恢复
	// progressing 下载进行中,可获取下载进度
     	if (state === 'interrupted') {
      	 // 可恢复下载
      	} else if (state === 'progressing') {
	        if (downloadItem.isPaused()) {
	        	console.log('Download is paused')
	        } else {
	        // 可以根据字节大小设置进度条
        	console.log(`Total bytes: ${downloadItem.getTotalBytes()}`)      
        	console.log(`Received bytes: ${downloadItem.getReceivedBytes()}`)
        	}
      	}
   	})
	
	item.once('done', (event, state) => {
	// 下载结束
      if (state === 'completed') {
      	// 下载成功
        console.log('Download successfully')
        // 用应用程序打开文件
        openFile(downloadItem.getSavePath())
      } else {
      	// state为cancelled或interrupted
      	// 下载取消或下载已经中断,无法恢复
        console.log(`Download failed: ${state}`)
      }
      // 这里可将下载结束的状态传回给渲染进程
      win.webContents.send('downstate', state)
    })
})

shell 使用默认应用程序管理文件和 url,提供与桌面集成相关的功能

shell.openPath(path) 以桌面的默认方式打开给定的文件。

javascript 复制代码
function openFile(filePath) {
	const path = require('path');
  	shell.openPath(path.join(filePath));
}
相关推荐
daols884 小时前
vue vxe-table 自适应列宽,根据内容自适应宽度的2种使用方式
vue.js·vxe-table
OEC小胖胖6 小时前
告别 undefined is not a function:TypeScript 前端开发优势与实践指南
前端·javascript·typescript·web
行云&流水6 小时前
Vue3 Lifecycle Hooks
前端·javascript·vue.js
老虎06277 小时前
JavaWeb(苍穹外卖)--学习笔记04(前端:HTML,CSS,JavaScript)
前端·javascript·css·笔记·学习·html
三水气象台7 小时前
用户中心Vue3网页开发(1.0版)
javascript·css·vue.js·typescript·前端框架·html·anti-design-vue
烛阴7 小时前
Babel 完全上手指南:从零开始解锁现代 JavaScript 开发的超能力!
前端·javascript
CN-Dust8 小时前
[FMZ][JS]第一个回测程序--让时间轴跑起来
javascript
盛夏绽放8 小时前
Vue3 中 Excel 导出的性能优化与实战指南
vue.js·excel
全宝9 小时前
🎨前端实现文字渐变的三种方式
前端·javascript·css
yanlele9 小时前
前端面试第 75 期 - 2025.07.06 更新前端面试问题总结(12道题)
前端·javascript·面试