我一起使用StreamSaver和NodeJS将10,000,000条记录保存到.Json格式的文件中。运行应用程序并下载了大量记录(200,000或300,000或....)后,下载已取消并中止file.json并在下载程序中显示错误“ 网络失败 ”浏览器。
为什么要显示此错误并取消下载?
注意:我在控制台中没有任何错误,我下载了大约500MB或更多...所以范围/内存没问题。
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");
// ... error checks
request.on('recordset', function (recordset) {
// Emitted once for each recordset in a query
});
request.on('row', function (record) {
// Emitted for each row in a recordset
socket.emit('recieve_records', record); //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
});
});
});
var fileStream = streamSaver.createWriteStream('filename.json');
writer = fileStream.getWriter();
encoder = new TextEncoder();
var counter = 0;
socket.on('recieve_records', function (data) {
var uint8array = encoder.encode(data + "\n"); // data='every things'
writer.write(uint8array);
counter++;
if (counter > 100000) {
console.log('100.000 messages downloaded');
counter = 0;
}
});