sindresorhus / electron-dl

Simplified file downloads for your Electron app
MIT License
1.16k stars 137 forks source link

MULTIPLY BUG with DOWNLOAD (BIGGEST BUG 2021?) #127

Open copyandexecute opened 3 years ago

copyandexecute commented 3 years ago

After I click on my button the download multiplies each team

ipcMain.on('download', (ev, args) => {
  const focusedWindow = BrowserWindow.getFocusedWindow()
  if (focusedWindow) {
    args.properties.onProgress = (status: number) => focusedWindow.webContents.send('download-progress', status)
    download(focusedWindow, args.url, args.properties)
      .then(dl => {
        console.log(ev)
        focusedWindow.webContents.send('download-complete', dl.getSavePath())
      })
      .catch(reason => {
        console.log(reason)
      })
  }
})
export const downloadAndWriteFile = (url: string, properties: electronDl.Options, cb?: any) => {
  ipcRenderer.send('download',
    {
      url: url,
      properties: { ...properties }
    })
  ipcRenderer.on('download-complete', (event, file) => {
    console.log(file)
    if (cb instanceof Function) {
      cb()
    }
  })
  ipcRenderer.on('download-progress', (event, progress) => {
    /* if (status) {
      status(Math.floor(progress.percent * 100) + '%')
    } */
    // console.log(progress)
  })
}
kah9509 commented 3 years ago

I have the same problem.... I try to receive 2 files in sequence, but the download event does not work intermittently...ㅠㅠ SOMEBODY HELPME

megakraken commented 3 years ago

This thing is just horrendously broken as everything electron.

megakraken commented 3 years ago

Because of the unbelievable crappiness of electron that manages to f*ck up even a most basic task like downloading a file, I have come up with this nodejs function that I use instead. Electron must be the only technology that manages to be even crappier than Node.js itself which is an incredible achievement in its own right, but whatever.

async function downloadFile(destPath, url) {
  let cookies = await session.defaultSession.cookies.get({}),
      header  = cookies.map(c => `${c.name}=${c.value}`).join('; '),
      _fs     = require('fs'),
      _http   = require('http');
  return new Promise((resolve, reject) => {
    _http.get(url, { headers: { 'Cookie': header } }, ret => {
      let file = _fs.createWriteStream(destPath);
      ret.pipe(file);
      file.on('finish', () => {
        file.close();
        resolve(destPath);
      });
    }).on('error', err => {
      reject(err.message);
    });
  });
}
theogravity commented 7 months ago

I think this is due to it spawning two listeners if you look at the source.

Give my package a try where it does handle multiple downloads:

https://github.com/theogravity/electron-dl-manager

xianyunleo commented 1 month ago

My library support Multiple downloads are handled properly and report individual progress. You are also able to cancel / pause / resume as well. Easy to use

https://github.com/xianyunleo/electron-dl-downloader