How to use electron-dl
Matthew Harrington
I am trying to get the package electron-dl working. For that, i use the standard electron-quick-start example project.
Unfortunately, applying the example code from electron-dl to the electon-quick-start example does not work for me, meaning nothing is happening and no errors in the browser-console or the terminal.
This is how i implemented the minimal example:
main.js
const {app, BrowserWindow, ipcMain} = require('electron')
const {download} = require('electron-dl');
let mainWindow
ipcMain.on('download-item', async (event, {url}) => { event.sender.send('download-success', url) console.log(url) const win = BrowserWindow.getFocusedWindow(); console.log(await download(win, url));
});
// ...renderer.js
const { ipcRenderer } = require('electron')
$('#btn-dl').click(() => { const newURL = "" ipcRenderer.send('download-item', {url: newURL})
})
ipcRenderer.on('download-success', (event, arg) => { console.log(arg)
})index.html
<h1>Hello World!</h1>
<button>Download</button>
<script>
// jQuery
$ = require('jquery')
// You can also require other files to run in this process
require('./renderer.js')
</script>In this implementation i am simply trying to download the file 5MB.zip when pressing the Download button.
What am i doing wrong?
Could someone please provide a simple working example of the implenentation of electron-dl using the electron-quick-start example?
Thanks for your help!
32 Answers
Having recreated exactly what you described, it works flawlessly for me.
The following is output on the console:
DownloadItem { _events: { updated: [Function], done: [Function] }, _eventsCount: 2 }The package determines the location to store the file itself, if you don't specify it. You can output the path it chooses by default using app.getPath('downloads'). For me, this is my home directory (Linux).
If you want to set the download directory yourself:
download(win, url, { directory: "/path/to/my/directory/"
})The package will create directories as needed.
1I would just like to add, I had spent a couple of days messing around with this issue. I encountered the exact same thing on my project. By disabling my antivirus shields, it stopped interrupting the downloads. Make sure you make an exception for your project / any visual studio code processes. I just chose to disable the shield as a whole.
Edit: do so at your own risk.