Convert stream into buffer?

Instead of piping, you can attach readable and end event handlers to the part stream to read it:

var buffers = [];
part.on('readable', function(buffer) {
  for (;;) {
    let buffer = part.read();
    if (!buffer) { break; }
    buffers.push(buffer);
  }
});
part.on('end', function() {
  var buffer = Buffer.concat(buffers);
  ...do your stuff...

  // write to file:
  fs.writeFile('image/' + part.filename, buffer, function(err) {
    // handle error, return response, etc...
  });
});

Note: If you instead use data, it will read the entire upload into memory.

You could also create a custom transform stream to transform the incoming data, but that might not be trivial.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)