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

相关推荐
IMPYLH9 小时前
HTML 的 <p> 元素
前端·javascript·html
leoZ2319 小时前
第 4 篇:布局骨架——页面壳、栅格、卡片
前端·javascript·vue.js·opencv·计算机视觉·数据挖掘·语音识别
何以解忧,唯有..9 小时前
Vue 3 模板语法详解:从插值到指令的完整指南
前端·javascript·vue.js
EasyGBS9 小时前
从接入到稳定播放:无插件直播H5视频流媒体播放器EasyPlayer.js如何撑起Web端流媒体
开发语言·前端·javascript
不可能片场9 小时前
nohup 之后进程还是死了:GUI 子进程的存活陷阱
前端·electron
Carl_奕然9 小时前
【智能体】Agent的四种设计模式之:React(2026最新版)
javascript·人工智能·python·react.js·设计模式·语言模型
名为沙丁鱼的猫7299 小时前
React/JSX 编码规范
前端·javascript·react.js
Highcharts.js10 小时前
甘特图纵向任务、横向时间显示图表可视化开发|Highcharts甘特图示例
开发语言·前端·javascript·可视化·甘特图·数据可视化·highcharts
烬羽10 小时前
给 Agent 一个检索式记忆:把对话历史存进 Milvus,该记的都记得
javascript·数据库·agent