gpt4 book ai didi

javascript - Electron will-download 不断被中断

转载 作者:行者123 更新时间:2023-12-03 12:22:34 25 4
gpt4 key购买 nike

我正在尝试下载文件,但它一直被中断,我不知道为什么。我也找不到任何关于如何调试它被中断的原因的信息。

这是我保存文件的位置:

C:\Users\rnaddy\AppData\Roaming\Tachyon\games\murware\super-chain-reaction\web.zip


window.webContents.session.on('will-download', (event, item, webContents) => {
let path = url.parse(item.getURL()).pathname;
let dev = path.split('/')[3] || null;
let game = path.split('/')[4] || null;
if (!dev && !game) {
item.cancel();
} else {
item.setSavePath(Settings.fileDownloadLocation(dev, game, 'web'));
item.on('updated', (event, state) => {
let progress = 0;
if (state == 'interrupted') {
console.log('Download is interrupted but can be resumed');
} else if (state == 'progressing') {
progress = item.getReceivedBytes() / item.getTotalBytes();
if (item.isPaused()) {
console.log('Download is paused');
} else {
console.log(`Received bytes: ${item.getReceivedBytes()}; Progress: ${progress.toFixed(2)}%`);
}
}
});
}
});

这是我的监听器,它将触发上述内容:
ipcMain.on(name, (evt) => {
window.webContents.downloadURL('http://api.gamesmart.com/v2/download/murware/super-chain-reaction');
});

这是我在控制台中得到的输出:
Received bytes: 0; Progress: 0.00%
Received bytes: 233183; Progress: 0.02%
Download is interrupted but can be resumed

我有一个主机文件设置:
127.0.0.1 api.gamesmart.com

当我尝试访问路径 http://api.gamesmart.com/v2/download/murware/super-chain-reaction在 chrome 中,文件可以很好地下载到我的下载文件夹中。那么,是什么原因造成的呢?

最佳答案

如果你设置了具体的下载目录,你应该使用完整的文件路径,文件名在item.setSavePath()中。方法。最好的方法是从 downloaditem 获取文件名对象( item 在你的情况下)本身。您可以使用 item.getFilename()轻松获取当前下载项目的名称。 here is the doc

还有一种很好的方法可以在 Electron 中获取常用的公共(public)系统目录路径。也就是说,使用 app.getPath(name)方法。 name将是几个目录的 Electron 预定义字符串。 here is the doc

因此,您的完整 setSavePath 函数将是 app.getPath("downloads") + "/" + item.getFilename()
在您的情况下,如果您对文件路径提取方法没问题,那么您唯一缺少的就是下载路径末尾的文件名。

当然,如果您愿意,您可以使用任何其他字符串作为文件名。但是请记住要放置正确的扩展名。 :)

关于javascript - Electron will-download 不断被中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41902103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com