electron-dl用于在Electron中下载多个文件

electron-dl用于在Electron中下载多个文件

javascript 复制代码
const { app, BrowserWindow, ipcMain } = require('electron');
const { download } = require('electron-dl');
const path = require('path');
 async function createWindow() {
  const mainWindow = new BrowserWindow();
   mainWindow.loadURL('https://example.com');
   mainWindow.webContents.on('did-finish-load', async () => {
    const files = [
      { url: 'https://example.com/file1.ext', directory: 'path/to/save/file1' },
      { url: 'https://example.com/file2.ext', directory: 'path/to/save/file2' },
      // Add more files with their respective URLs and directories
    ];
     for (const file of files) {
      const options = {
        directory: path.join(app.getPath('downloads'), file.directory),
      };
       try {
        const dl = await download(mainWindow, file.url, options);
        console.log(`File saved to: ${dl.getSavePath()}`);
      } catch (error) {
        console.error('File download failed:', error);
      }
    }
     mainWindow.close();
  });
}
 app.on('ready', createWindow);

在这个更新的代码中,我们使用了 electron-dl 模块的 download 函数来实现文件下载。我们在主窗口加载完成后,通过循环遍历文件列表,使用 await 关键字等待文件下载完成。下载成功后,我们打印出文件保存的路径。如果下载失败,则打印错误信息。

请注意,您需要在 files 数组中添加要下载的文件的URL和目录。确保您已经安装了 electron-dl 模块。

npm install electron-dl

相关推荐
前端怎么个事1 小时前
框架的源码理解——V3中的ref和reactive
前端·javascript·vue.js
不爱吃饭爱吃菜2 小时前
uniapp微信小程序一键授权登录
前端·javascript·vue.js·微信小程序·uni-app
heart000_12 小时前
从零开始打造个人主页:HTML/CSS/JS实战教程
javascript·css·html
90后小陈老师3 小时前
3D个人简历网站 5.天空、鸟、飞机
前端·javascript·3d
chenbin___3 小时前
react native text 显示 三行 超出部分 中间使用省略号
javascript·react native·react.js
漫路在线6 小时前
JS逆向-某易云音乐下载器
开发语言·javascript·爬虫·python
BillKu8 小时前
Vue3 Element Plus 对话框加载实现
javascript·vue.js·elementui
初遇你时动了情9 小时前
html js 原生实现web组件、web公共组件、template模版插槽
前端·javascript·html
前端小崔9 小时前
从零开始学习three.js(18):一文详解three.js中的着色器Shader
前端·javascript·学习·3d·webgl·数据可视化·着色器
运维@小兵9 小时前
vue配置子路由,实现点击左侧菜单,内容区域显示不同的内容
前端·javascript·vue.js