通过NodeJS下载Tar文件 [英] Download Tar File via NodeJS

查看:125
本文介绍了通过NodeJS下载Tar文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个nodejs http服务器,一个请求另一个tar文件。它通过浏览器测试工作正常,但我永远无法让第二台服务器正确粘合块。我对fwrite的尝试一直没用,

I have two nodejs http servers, one requests a tar file from the other. It works fine via browser testing, but I can never get the second server to glue the chunks together correctly. My attempts with fwrite has been as useless as this

// Receives File
var complete_file = '';
response.on('data', function(chunk){
   complete_file += chunk 
}).on('end', function(){
    fs.writeFile('/tmp/test.tgz', complete_file, 'binary')
});

// Send File
fs.readFile('/tmp/test_send.tgz', function(err, data){
    if (err) throw err;
    response.writeHead('200', {
        'Content-Type' : 'application/x-compressed',
        'Content-Length' : data.length
    });
    response.write(data);
    response.end();
});


推荐答案

我设法让它工作但我使用相反,这是客户代码:

I've managed to make it work but I use a writeable stream instead, this is the client code:

fs = require ('fs');

var http = require('http');
var local = http.createClient(8124, 'localhost');
var request = local.request('GET', '/',{'host': 'localhost'});
request.on('response', function (response) {
    console.log('STATUS: ' + response.statusCode);
    var headers = JSON.stringify(response.headers);
    console.log('HEADERS: ' + headers);
    var file = fs.createWriteStream('/tmp/node/test.gz');
    response.on('data', function(chunk){
        file.write(chunk);
        }).on('end', function(){
          file.end();
          });
    });
request.end();

这篇关于通过NodeJS下载Tar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆