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));
}
相关推荐
三十而立洋22 分钟前
JavaScript 原型链:一张图讲透「对象继承」的底层真相
前端·javascript
平头哥技术团队27 分钟前
Day 13 | 调 line-height 和 margin:三处间距让名片页脱离模板感
开发语言·前端·javascript·学习·html5
老三_Micheal1 小时前
一文搞懂 Axios 请求参数详解:headers、params 与 body 的区别和使用场景
javascript
研☆香2 小时前
js中 onload 事件的用法
开发语言·javascript·ecmascript
志尊宝2 小时前
Vue3 零基础每日笔记(005):ref 响应式基础——为什么 script 里要 .value
前端·javascript·vue.js·笔记
yyt3630458412 小时前
一次鼠标交互触发两次 Vue flushJobs:把高频交互从 VDOM 里拆出来
前端·vue.js·计算机外设
知兀3 小时前
框架脚手架搭建,推送github一键使用
javascript·vue.js·ecmascript
mayaairi3 小时前
Vue2 实战进阶:表单数据收集、指令系统与自定义指令全解析
前端·javascript·vue.js
会周易的程序员3 小时前
告别 matiec 与 Docker:aiDgePLC Editor 全面拥抱 STVM 字节码编译
物联网·网关·electron·软plc·iec61131·stvm·open plc
图扑软件4 小时前
下篇・换墨|主题/多语言/移动端,一套组件全覆盖
前端·javascript·ui·性能优化·数据可视化