A simple module to download files via Electron's main process. Manages the download process and exposes a simple API to listen for events including progress, success, failure, and cancellation. The module also exposes Electron's DownloadItem
methods for pausing, resuming, and canceling the download.
npm install electron-file-download
const { FileDownload } = require('electron-file-download');
// Download will start automatically when instantiated
const download = new FileDownload(
"https://example.com/file.txt",
"/download-directory"
);
download.on('progress', (progress) => {
console.log(`Download progress: ${progress.percent * 100}%`);
});
download.on('success', (filePath) => {
console.log(`Download complete: ${filePath}`);
});
download.on('error', (error) => {
console.log(`Download failed: ${error.message}`);
});
download.on('cancel', () => {
console.log(`Download canceled`);
});
Creates a new download instance. The download will start automatically when instantiated.
Type: string
The URL to download.
Type: string
The directory to save the file to.
Type: object
An Electron Session
instance to use for the download. Defaults to session.fromPartition('file-download')
.
##### `dependencies.logger` (optional)
A `console`-compatible logger (such as electron-log or winston) to use when debugging the module.
```javascript
const download = new FileDownload(
"https://example.com/file.txt",
"/download-directory",
{
logger: console
}
);
Type: string
The event to listen for. Can be one of the following:
progress
: Emitted when the download progress changes.completed
: Emitted when the download completes successfully.cancelled
: Emitted when the download is cancelled.paused
: Emitted when the download is paused.resumed
: Emitted when the download is resumed.error
: Emitted when the download fails.
Returns a Promise
that resolves with the path of the downloaded file when the download completes successfully or fails.
Pauses the download.
Resumes the download.
Cancels the download.
A Promise-like object that resolves to the Electron DownloadItem
instance when the download starts. @see https://www.electronjs.org/docs/api/download-item
const download = new FileDownload(
"https://example.com/file.txt",
"/download-directory"
);
const mimeType = (await download.downloadItem).getMimeType();
MIT
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.