首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >通过请求获取Node.js格式的下载进度

通过请求获取Node.js格式的下载进度
EN

Stack Overflow用户
提问于 2013-08-20 05:08:06
回答 6查看 42.1K关注 0票数 36

我正在创建一个使用Node模块request下载应用程序文件的更新程序。如何使用chunk.length估计剩余的文件大小?下面是我的部分代码:

var file_url = 'http://foo.com/bar.zip';
var out = fs.createWriteStream('baz.zip');

var req = request({
    method: 'GET',
    uri: file_url
});

req.pipe(out);

req.on('data', function (chunk) {
    console.log(chunk.length);
});

req.on('end', function() {
    //Do something
});
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2013-08-20 06:28:59

这应该会让你得到你想要的总数:

req.on( 'response', function ( data ) {
    console.log( data.headers[ 'content-length' ] );
} );

我得到的内容长度是9404541

票数 28
EN

Stack Overflow用户

发布于 2013-11-26 04:51:44

function download(url, callback, encoding){
        var request = http.get(url, function(response) {
            if (encoding){
                response.setEncoding(encoding);
            }
            var len = parseInt(response.headers['content-length'], 10);
            var body = "";
            var cur = 0;
            var obj = document.getElementById('js-progress');
            var total = len / 1048576; //1048576 - bytes in  1Megabyte

            response.on("data", function(chunk) {
                body += chunk;
                cur += chunk.length;
                obj.innerHTML = "Downloading " + (100.0 * cur / len).toFixed(2) + "% " + (cur / 1048576).toFixed(2) + " mb\r" + ".<br/> Total size: " + total.toFixed(2) + " mb";
            });

            response.on("end", function() {
                callback(body);
                obj.innerHTML = "Downloading complete";
            });

            request.on("error", function(e){
                console.log("Error: " + e.message);
            });

        });
    };
票数 21
EN

Stack Overflow用户

发布于 2017-02-16 19:47:17

如果您使用的是request模块,并且希望在不使用任何额外模块的情况下显示下载百分比,您可以使用以下代码:

function getInstallerFile (installerfileURL,installerfilename) {

    // Variable to save downloading progress
    var received_bytes = 0;
    var total_bytes = 0;

    var outStream = fs.createWriteStream(installerfilename);
    
    request
        .get(installerfileURL)
            .on('error', function(err) {
                console.log(err);
            })
            .on('response', function(data) {
                total_bytes = parseInt(data.headers['content-length']);
            })
            .on('data', function(chunk) {
                received_bytes += chunk.length;
                showDownloadingProgress(received_bytes, total_bytes);
            })
            .pipe(outStream);
};

function showDownloadingProgress(received, total) {
    var platform = "win32"; // Form windows system use win32 for else leave it empty
    var percentage = ((received * 100) / total).toFixed(2);
    process.stdout.write((platform == 'win32') ? "\033[0G": "\r");
    process.stdout.write(percentage + "% | " + received + " bytes downloaded out of " + total + " bytes.");
}

用法:

getInstallerFile("http://example.com/bar.zip","bar.zip");
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18323152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档