Access downloadItem.pause()


#1

how could i call item.pause out of session.on(‘will-download’) because it is function of item inside session.on(‘will-download’)?

var url, path, filename;
ipcMain.on(‘item:file’, function(e, file) {
if(file.msg === ‘d’){
console.log(file);
url = file.url;
path = pathLib.join(__dirname, file.path);
filename = file.filename;
contents.downloadURL(url);
}
if(file.msg === ‘p’){
item.pause();
}
if(file.msg === ‘r’){
item.resume();
}

contents.session.on('will-download', (event, item, contents) => {   
  // Set the save path, making Electron not to prompt a save dialog.
  path = `${path}${filename}`;
  item.setSavePath(path);
  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: ${item.getReceivedBytes()} out of ${item.getTotalBytes()}`);
        var percentage  = (item.getReceivedBytes()/item.getTotalBytes())*100;
        var progress = {'rBytes':item.getReceivedBytes(), 'tBytes':item.getTotalBytes(), 'p':percentage};
        contents.send('item:progress', progress);
      }
    }
  });
  item.once('done', (event, state) => {
    if (state === 'completed') {
      console.log('Download successfully');
    } else {
      console.log(`Download failed: ${state}`);
    }
  });
});

});


#2

Worked for me!