Electron-vue开发实战 - 文件下载及进度显示

主要通过 Electron 中 will-download 事件和主进程和渲染进程之间的通信,实时监听下载进度main/index.jsmainWindow.webContents.session.on('will-download', (e, item) => { //获取文件的总大小 const totalBytes = item.getTotalBytes(); //设置文件的保存路径,此时默认弹出的 save dialog 将被覆盖 const rootPa
摘要由CSDN通过智能技术生成

主要通过 Electronwill-download 事件和主进程和渲染进程之间的通信,实时监听下载进度

main/index.js

mainWindow.webContents.session.on('will-download', (e, item) => {
   
    //获取文件的总大小
    const totalBytes = item.getTotalBytes();
    //设置文件的保存路径,此时默认弹出的 save dialog 将被覆盖
    const rootPath = app.getPath("userData") + "/channel";
    const filePath = path.join(rootPath, item.getFilename());
    item.setSavePath(filePath);
  	//获得下载链接
    let url = item.getURL()

    //监听下载过程,计算并设置进度条进度
    item.on('updated', () => {
   
      let process = item.getReceivedBytes() / totalBytes
      process = Math.round(process * 100)
      mainWindow.setProgressBar(process);
      //向渲染进程发送下载进度
      sendMessage(url, process)
    });
    //监听下载结束事件
    item.on('done', (e, state) => {
   
      //如果窗口还在的话,去掉进度条
      if (!mainWindow.isDestroyed()) {
   
        mainWindow.setProgressBar(-1);
      }

      //下载被取消或中断了
      if (state == 'interrupted') {
   
        electron.dialog.showErrorBox('下载失败', `文件 
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值