V
V
Vladimir Krauz2016-09-08 00:47:40
JavaScript
Vladimir Krauz, 2016-09-08 00:47:40

NodeJS How to write data received from busboy to Buffer?

Good evening.
I use busboy to get data (pictures).
I get data like this:

var buffer = Buffer.allocUnsafe(filesize);
req.recvSize = 0;  
 file.on('data', function (data) {
            buffer.write(data,req.recvSize,data.length);
            req.recvSize += data.length;
 });

The output is this error:
buffer.js:761
return this.utf8Write(string, offset, length);
^
TypeError: Argument must be a string
at TypeError (native)
at Buffer.write (buffer.js:761:21
) for this and decided to immediately write to the buffer).
How can I solve such an error or how will it be more correct to receive data?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Sergey, 2016-09-08
@dev_random

you can use Buffer.copy instead of write.
https://nodejs.org/dist/latest-v6.x/docs/api/buffe...
or switch file-stream to the mode when it will send utf8 strings through the "data" event.
https://nodejs.org/dist/latest-v6.x/docs/api/strea...
Upd: It's a bad idea to read utf8 cards in strings. so -> buffer.copy()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question