How to access item in DownloadItem?


#1

I am using following for downloading files on a ipc call:

win.webContents.session.on('will-download', (event, item, webContents) => {

    let name = item.getFilename();
    item.on('updated', (event, state) => {
      if (state === 'interrupted') {
        console.log('Download is interrupted but can be resumed')
      } else if (state === 'progressing') {
        if (item.isPaused()) {
          console.log('Download is paused')
        } else {
          console.log(`Received bytes: ${name} ---- ${item.getReceivedBytes()}`)
        }
      }
    })
    item.once('done', (event, state) => {
      if (state === 'completed') {
        console.log('Download successfully')
      } else {
        console.log(`Download failed: ${state}`)
      }
    })
  })

win.webContents.downloadURL(url)

How can I access the item in the downloadItem so that I can pause, cancel or resume the download item from an ipc call? (as well as get the the download info such as progress and time remaining etc.)
And if I close the app, will the download remains in the session? So, when I open up the app again, the download items is still in download queue, like the one in chrome download manager? If not, do I need to store them in db?

In a nutshell, I just want to create a download manager like the one in the chrome default download manager, except the download item pop up in the bottom of the window, but display theme in a tab.


#2

I have the same question. can anyone help?

  • Electron version: 1.4.3
  • Operating system: win7
  • Description:

I am working on a download function using electron + angularjs. it can download file successfully.
However I do not know how to pause a download, can anyone help?

in main.js

ipcMain.on('dowloadFiles', function(evt, files) {
        var wc = mainWindow.webContents;
        for (var i = files.length - 1; i >= 0; i--) {
            var file = files[i];
            wc.downloadURL(file.url);
        }
        
    });

    mainWindow.webContents.session.on('will-download', function(event, item, webContents) {
      // Set the save path, making Electron not to prompt a save dialog.
      item.setSavePath('c:\\downloadTest\\test.txt');

      item.on('updated', function(event, state) {
        if (state === 'interrupted') {
          console.log('Download is interrupted but can be resumed');
        } else if (state === 'progressing') {
          if (item.isPaused()) {
            console.log('Download is paused');
          } else {
            console.log('Received bytes: ', item.getReceivedBytes());
          }
        }
      });
      item.once('done', function(event, state) {
        if (state === 'completed') {
          console.log('Download successfully');
        } else {
          console.log('Download failed: ', state);
        }
      });
    });

in html page

<li ng-click="download('http://server/a.txt')">a.txt</li>

var ipc = require('electron').ipcRenderer;
$scope.download = function(url) {
   ipc.send('dowloadFiles', [file]);
}