A
A
adsprosto2017-03-07 23:04:54
JavaScript
adsprosto, 2017-03-07 23:04:54

What's going on with stream.read()?

In nodejs recently and I can’t really go deep due to lack of documentation and translation to the existing one, for my task I need to convert information, but I ran into such a problem: The
following code is written
stream = fs.createReadStream(path);
stream.on('readable', function() {
var data;
while((data = stream.read(2187)) != null){
fs.appendFileSync('local', data);
}
}).on(' end', function() {
}).on('error',function(er){
$('#FileSave').html(er);
}).on('close', function() {
$(' #preloader').fadeOut(500);
stream.close();
});
Everything works well, but at some point it can stupidly stop, a file of 23 megabytes is taken, the stream can work with a bang, or it can “get stuck” somewhere and not reach the end corny, there were times when a quarter of the file was processed or half, and sometimes it still works fully, tell me what's wrong?
Main goal: to take characters, for example, encrypt, write to a file.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2017-03-07
@bingo347

var read = fs.createReadStream(path);
var write = fs.createWriteStream('local');

write.on('error', function(err) {
  //ошибка записи
});
read.on('error', function(err) {
  //ошибка чтения
});
read.on('end', function() {
  //завершили чтение и запись
})

//проксируем поток чтения в поток записи
read.pipe(write);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question