# electron-download-manager
> Manage downloadItems from [Electron](https://electronjs.org)'s BrowserWindows without user interaction, allowing single file download and bulk downloading asynchronously
## Why?
- Register global listener that attaches to all newly created BrowserWindow instances
- Automatically download the file to a given folder, without user prompt
- Callback when download has completed (or failed)
- Bulk download: pass a bunch of links and all will get downloaded, with a callback once they're all done.
## Install
```
$ npm install electron-download-manager
```
## Usage
### Register it for all windows
Register the listener (that will catch all DownloadItems)
```js
const electron = require("electron");
const { app, BrowserWindow } = electron;
const DownloadManager = require("electron-download-manager");
DownloadManager.register();
app.on("ready", () => {
let mainWindow = new BrowserWindow();
});
```
### Examples
After registering you must wait until at least 1 window is created to call DownloadManager.download function
#### Single file download from the Main Process
```js
const electron = require("electron");
const { app, BrowserWindow } = electron;
const DownloadManager = require("electron-download-manager");
DownloadManager.register({
downloadFolder: app.getPath("downloads") + "/my-app"
});
app.on("ready", () => {
let mainWindow = new BrowserWindow();
mainWindow.loadURL(`file://${__dirname}/app/index.html`);
//Single file download
DownloadManager.download({
url: "https://i.imgur.com/H124sSq.jpg"
}, function (error, info) {
if (error) {
console.log(error);
return;
}
console.log("DONE: " + info.url);
});
});
```
This example downloads *https://i.imgur.com/H124sSq.jpg* file to *user-downloads-folder/my-app/H124sSq.jpg*
#### Bulk file download from the Main Process
```js
const electron = require("electron");
const {app, BrowserWindow} = electron;
const DownloadManager = require("electron-download-manager");
DownloadManager.register({downloadFolder: app.getPath("downloads") + "/my-app"});;
app.on("ready", () => {
let mainWindow = new BrowserWindow();
mainWindow.loadURL(`file://${__dirname}/app/index.html`);
var links = [
"https://i.imgur.com/xineeuw.jpg",
"https://i.imgur.com/RguiWa6.jpg",
"https://i.imgur.com/JR4Z0aD.jpg",
"https://i.imgur.com/ccvEJO1.jpg",
"https://i.imgur.com/yqZoShd.jpg"
];
//Bulk file download
DownloadManager.bulkDownload({
urls: links,
path: "bulk-download"
}, function (error, finished, errors) {
if (error) {
console.log("finished: " + finished);
console.log("errors: " + errors);
return;
}
console.log("all finished");
});
});
```
This example downloads 5 files to *user-downloads-folder/my-app/bulk-downloads*
#### Use from Renderer Process
Once you've registered the listener on the Main process at any time you can call the download function through electron's [`remote`](https://electronjs.org/docs/api/remote)
```js
require("electron").remote.require("electron-download-manager").download({
url: "https://i.imgur.com/H124sSq.jpg"
}, function (error, info) {
if (error) {
console.log(error);
return;
}
console.log("DONE: " + info.url);
});
```
## API
### DownloadManager.register([options])
### options
#### downloadFolder
Type: `string`<br>
Default: `app.getPath("downloads")]`
Set a folder where all downloadItems will be downloaded to. It will also be the parent folder for individual folders of each download. Explained below in Download function.
By default, this "root" folder will be user's OS downloads folder
([read about this](http://electron.atom.io/docs/api/app/#appgetpathname))
If the file already exists in the location it will check the file's size against the size on the server, if it is lower than the server it will attempt to resume downloading the file. This is good for downloading large files. E.G Downloading a 200MB file and only 100MB downloaded (app closed/crashed) it will resume the download from where it left off automatically.
If the file size on the disk is the same as the server it will not download and return a successful callback.
### DownloadManager.download(options, callback(error, {url, filePath}))
### options
#### url
Type: `string`
The url of the file to be downloaded
#### path
Type: `string`<br>
Default: `""`
Set a folder where this downloadItems will be downloaded to. This folder is relative to downloadFolder location set in the register function. By default it will be downloaded to root of downloadFolder which would be user download's folder.
#### onLogin(authInfo, callback)
Type: `function`<br>
Emitted when an authenticating proxy is asking for user credentials.
```js
DownloadManager.download({
url: "https://i.imgur.com/H124sSq.jpg",
onLogin: (authInfo, callback) => {
callback('username', 'password');
},
}, function (error, info) {
if (error) {
console.log(error);
return;
}
console.log("DONE: " + info.url);
});
```
> Please see [Class ClientRequest's 'login' event](http://electronjs.org/docs/api/client-request#event-login) for detail.
#### onProgress(progress, item)
Type: `function`<br>
A function to be called whenever the file being downloaded progresses, this function will be constantly called with the updated value.
`progress` an object with various metrics for the downloading file
```
{
downloaded // downloaded amount in ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
downloadedBytes // same as previous only in bytes for calculation if required (eg. 4061073)
progress // float progress of downloaing file (0 to 100)
remaining // remaining amount of file download in ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] (eg. 28.36 MB)
remainingBytes // same as previous only in bytes for calculation if required (eg. 28360919)
speed // download speed (eg. 311.3 KB/sec)
speedBytes // same as previous only in bytes for calculation if required (eg. 311296)
total // file size in ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] (eg. 32.42 MB)
totalBytes // same as previous only in bytes for calculation if required (eg. 32421992)
}
```
`item` instance of [DownloadItem](https://electronjs.org/docs/api/download-item#class-downloaditem) class. Useful for pausing and cancelling among other things.
> This feature currently exists only for single file downloads and hasn't been implemented (yet) for bulk processing.
### callback(error, {url, filePath})
Callback to be called when the download has reached a "done" state, which could mean two things either it was successful, or it failed.
if the download was successful the callback's error will be `null`, otherwise it will contain the error message
`url` returns the url of the downloaded file<br>
`filePath` location of where the file was saved
### DownloadManager.bulkDownload(options, callback(error, finished, failed))
### options
#### urls
Type: `array`
Array of `url` strings of the files to be downloaded
#### path
Type: `string`<br>
Default: `""`
Set a path to save all the bulk downloaded files. This folder is relative to downloadFolder location set in the register function. By default it will be downloaded to root of downloadFolder which would be user download's folder.
#### onResult(finishedCount, errorsCount, itemUrl)
Type: `function`<br>
A function to be called whenever a file has been downloaded or whenever a download failed.
`finishedCount` integer. Represents the number of file successfully downloaded. example: `4`
`errorsCount` integer. Represents the number
没有合适的资源?快使用搜索试试~ 我知道了~
electronic-download-manager:无需用户交互即可从Electron的BrowserWindows管理下载...
共6个文件
jshintrc:1个
license:1个
js:1个
需积分: 50 8 下载量 99 浏览量
2021-02-04
05:07:05
上传
评论 2
收藏 8KB ZIP 举报
温馨提示
电子下载管理器 无需用户交互即可从的BrowserWindows管理下载项,从而允许单文件下载和异步批量下载 为什么? 注册附加到所有新创建的BrowserWindow实例的全局侦听器 自动将文件下载到给定文件夹,而无需用户提示 下载完成(或失败)时回调 批量下载:传递一堆链接,所有链接都将被下载,一旦完成,便会回调。 安装 $ npm install electron-download-manager 用法 注册所有窗口 注册侦听器(它将捕获所有DownloadItems) const electron = require ( "electron" ) ; const { app , B
资源详情
资源评论
资源推荐
收起资源包目录
electron-download-manager-master.zip (6个子文件)
electron-download-manager-master
.jshintrc 208B
.gitignore 578B
package.json 849B
LICENSE 1KB
index.js 9KB
README.md 9KB
共 6 条
- 1
黄荣钦
- 粉丝: 33
- 资源: 4539
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0