Javascript - 下载流数据

时间:2017-02-06 11:21:48

标签: javascript node.js stream

我通过流方法从服务器(NodeJS)获取数据(2GB),现在我想在浏览器(客户端)上下载这些数据,但收到的数据正在增加并使用大量内存(RAM)但我不希望这样。换句话说,我想在浏览器(RAM)中将此数据保存在硬盘驱动器上。

app.js

var http = require('http');
var express = require('express');
var app = express();
var server = http.Server(app);
var io = require('socket.io')(server);

io.on('connection', function (socket) {
    console.log('Connection is ready!')
    socket.on('get_records', function (data) {
    var connection = new sql.Connection(config, function (err) {
        if (err) {
            console.log(err.message);
        }

        var request = new sql.Request(connection);
        request.stream = true;

        request.query("Select * from my_table"); //2GB data
        // ... error checks
        request.on('recordset', function (recordset) {
            // Emitted once for each recordset in a query
        });

        request.on('row', function (message) {
            // Emitted for each row in a recordset
            socket.emit('recieve_records', message); //send record by record to client
        });

        request.on('error', function (err) {
            console.log(err.message);
        });

        request.on('done', function (returnValue) {
            // Always emitted as the last one
        });
    });
});

client.js

socket.on('recieve_records', function (data) {
    email.push(data);
});

0 个答案:

没有答案