Nodejs发送多个文件

时间:2015-10-14 13:20:08

标签: node.js

我尝试将多个文件发送到已使用的文件并显示它们,现在我正在使用简单的txt文件进行测试。 由于一次管道超过10个文件会导致警告/错误,我试图一次只管道10个。函数被调用了适当的次数,但由于某种原因,只发送部分数据(似乎总是停在2.3mb)。有什么理由吗?

  app.get('/file', function(req, res)  {
    console.time('leggo');
    var lowerbound = req.param('lowerbound'); // 3 vars used for the file.
    var upperbound = req.param('upperbound');
    var filename = req.param('filename');
    res.writeHead(200, {
        'Content-type' : 'text/plain',
        'Content-disposition' : 'inline'
    });
    var readst = fs.createReadStream(base+ lowerbound + last); //first file.
    var count = 0; // number of files that are being sent atm.

    var add = function(i) { // add i'th file to the pipe
        console.log('whatevs');
        ++i;
        readst.push(base + i + last);
    };
    var started = 0;
    function sendem() { //send files.
        ++count;
        ++started;
        var mycallback = function () { //callback when doit finished
            console.log(count + ' ' + started + '\n');
            count -= 1;
            if (started == upperbound)
                return;
            if (count < 10) sendem();
        };
        function doit(mycallback) { //send a file then call callback
            add(started);
            mycallback();
        };
        doit(mycallback); //start sending.
}

    sendem(); // start the "main" sending function.
    readst.pipe(res); // pipe everything, also tried using this earlier, same effect.
    readst.on('end', function(err,data) { // "everything" was sent.
        console.log('done');
        console.timeEnd('leggo');
    });
});

0 个答案:

没有答案