Electron / node.js,无法以任何方式下载文件

问题描述

我是开发人员的新手,我在网上搜索了很多东西,但我认为我的项目有问题,我尝试从任何地址下载文件的所有操作均不起作用。

我在Mac上启动了项目,一切正常,我按照此步骤下载了文件https://ourcodeworld.com/articles/read/228/how-to-download-a-webfile-with-electron-save-it-and-show-download-progress

它确实工作得很好,但是知道,我不得不在窗户上工作,而且此功能已不再起作用,我尝试的所有操作,下载都为0字节。

return new Promise(function (resolve,reject) {
    // Save variable to kNow progress
    let received_bytes = 0;
    let total_bytes = 0;
    let req = request({
        method: 'GET',uri: configuration.remoteFile
    });

    let out = fs.createWriteStream(configuration.localFile);
    req.pipe(out);

    req.on('response',function (data) {
        // Change the total bytes value to get progress later.
        total_bytes = parseInt(data.headers['content-length']);
    });

    // Get progress if callback exists
    if (configuration.hasOwnProperty("onProgress")) {
        req.on('data',function (chunk) {
            // Update the received bytes
            received_bytes += chunk.length;

            configuration.onProgress(received_bytes,total_bytes);
        });
    } else {
        req.on('data',function (chunk) {
            // Update the received bytes
            received_bytes += chunk.length;
        });
    }

    req.on('end',function () {
        let stringDownload = 'fichiers téléchargés';
        updateVisualStatus(total,number,stringDownload);
        resolve();
    });
});

`

我尝试用console.log记录此函数中发生的所有事件,但似乎并没有在Windows上进行,请问为什么? 或者,如果soemone有更好的选择来下载文件。非常感谢,

通过这种方式,当我要重载数组时我会调用函数

for (let i = 0; i < arrayTodownload.length; i++) {

    //Todo UNCOMMENT THIS
    if (arrayTodownload[i] !== "blank") {
        if (arrayTodownload[i].startsWith('http') === false) {
            await downloadFile({
                remoteFile: serverAdress + arrayTodownload[i],localFile: fileFolder + arrayTodownload[i],onProgress: await function (received,total) {
                    let percentage = (received * 100) / total;
                    console.log(percentage + "% | " + received + " bytes out of " + total + " bytes.");
                }
            },arrayTodownload.length,i).then(function () {
                if (i === arrayTodownload.length - 1) {
                    storage.removeItem('arrayTodownload');
                }
            });
        }
    }
}

如果执行此操作,它将仅在我的文件夹中创建一个0字节的文件

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)