electron-dl 创建了一个类,用户管理多个下载器,但是我无法拿到每个下载器的进度条。 // 下载器类 class Downloader { constructor(data, options) { this.data = data; this.options = options; this.item = null; this.onStart = null; this.onProgress = null; this.onCompleted = null; } start() { const win = BrowserWindow.getFocusedWindow(); this.options.onStarted = (item) => { this.item = item; if (this.onStart) { this.onStart(); } }; this.options.onProgress = (progress) => { if (this.onProgress) { this.onProgress(progress); } }; this.options.onCompleted = () => { if (this.onCompleted) { this.onCompleted(); } }; download(win, this.data.dowloadLink, this.options); } pause() { if (this.item) { this.item.pause(); } } resume() { if (this.item) { this.item.resume(); } } cancel() { if (this.item) { this.item.cancel(); } } }
时间: 2024-02-20 21:02:19 浏览: 81
Electron:从零开始写一个记事本app
如果你想要获取每个下载器的进度条,你可以在 Downloader 类中添加一个 progress 属性,用于存储每个下载器的进度。然后在 onProgress 回调函数中更新该属性的值。这样,你就可以通过调用 Downloader 实例的 progress 属性来获取每个下载器的进度条了。以下是修改后的代码示例:
```
class Downloader {
constructor(data, options) {
this.data = data;
this.options = options;
this.item = null;
this.onStart = null;
this.onProgress = null;
this.onCompleted = null;
this.progress = 0; // 新增 progress 属性
}
start() {
const win = BrowserWindow.getFocusedWindow();
this.options.onStarted = (item) => {
this.item = item;
if (this.onStart) {
this.onStart();
}
};
this.options.onProgress = (progress) => {
this.progress = progress; // 更新 progress 属性
if (this.onProgress) {
this.onProgress(progress);
}
};
this.options.onCompleted = () => {
if (this.onCompleted) {
this.onCompleted();
}
};
download(win, this.data.dowloadLink, this.options);
}
pause() {
if (this.item) {
this.item.pause();
}
}
resume() {
if (this.item) {
this.item.resume();
}
}
cancel() {
if (this.item) {
this.item.cancel();
}
}
}
```
阅读全文