如何在Node.js中一一下载文件?

时间:2019-08-05 16:07:59

标签: javascript node.js async-await node-modules requestjs

我正在尝试使用请求库下载多个文件,我需要一个一个地下载它们,并显示一个进度条,文件链接存储在一个数组中,该数组将它们传递给函数以开始下载

const request = require('request')
const fs = require('fs')
const ProgressBar = require('progress')

async function downloadFiles(links) {
    for (let link of links) {
        let file = request(link)
        file.on('response', (res) => {
            var len = parseInt(res.headers['content-length'], 10);
            console.log();
            bar = new ProgressBar('  Downloading [:bar] :rate/bps :percent :etas', {
                complete: '=',
                incomplete: ' ',
                width: 20,
                total: len
            });
            file.on('data', (chunk) => {
                bar.tick(chunk.length);
            })
            file.on('end', () => {
                console.log('\n');
            })
        })
        file.pipe(fs.createWriteStream('./downloads/' + Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)))
    }
}


let links = ['https://speed.hetzner.de/100MB.bin', 'https://speed.hetzner.de/100MB.bin', 'https://speed.hetzner.de/100MB.bin', 'https://speed.hetzner.de/100MB.bin']
downloadFiles(links)

到目前为止,这是我所得到的,问题是请求是异步的,我尝试使用async / await,但是那样我无法使进度条正常工作。 如何做到这一点,以便一次下载一个文件并有一个进度条?

1 个答案:

答案 0 :(得分:1)

根据我对async.queue的评论,这就是我的写法。 您可以根据需要多次调用dl.downloadFiles([]),它只会一次又一次地提取添加到队列中的所有内容。

const request = require('request')
const async = require('async')
const fs = require('fs')
const ProgressBar = require('progress')

class Downloader {
    constructor() {
        this.q = async.queue(this.singleFile, 1);

        // assign a callback
        this.q.drain(function() {
            console.log('all items have been processed');
        });

        // assign an error callback
        this.q.error(function(err, task) {
            console.error('task experienced an error', task);
        });
    }

    downloadFiles(links) {
        for (let link of links) {
            this.q.push(link);
        }
    }

    singleFile(link, cb) {
        let file = request(link);
        let bar;
        file.on('response', (res) => {
            const len = parseInt(res.headers['content-length'], 10);
            console.log();
            bar = new ProgressBar('  Downloading [:bar] :rate/bps :percent :etas', {
                complete: '=',
                incomplete: ' ',
                width: 20,
                total: len
            });
            file.on('data', (chunk) => {
                bar.tick(chunk.length);
            })
            file.on('end', () => {
                console.log('\n');
                cb();
            })
        })
        file.pipe(fs.createWriteStream('./downloads/' + Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)))
    }
}

const dl = new Downloader();

dl.downloadFiles([
    'https://speed.hetzner.de/100MB.bin',
    'https://speed.hetzner.de/100MB.bin'
]);