Create image from ArrayBuffer in Nodejs

Yaan picture Yaan · Mar 25, 2016 · Viewed 10.3k times · Source

I'm trying to create an image file from chunks of ArrayBuffers.

all= fs.createWriteStream("out."+imgtype);

for(i=0; i<end; i++){
    all.write(picarray[i]);
}

all.end();

where picarray contains ArrayBuffer chunks. However, I get the error
TypeError: Invalid non-string/buffer chunk.

How can I convert ArrayBuffer chunks into an image?

Answer

Nick Jennings picture Nick Jennings · Mar 30, 2016

Have you tried first converting it into a node.js. Buffer? (this is the native node.js Buffer interface, whereas ArrayBuffer is the interface for the browser and not completely supported for node.js write operations).

Something along the line of this should help:

all= fs.createWriteStream("out."+imgtype);

for(i=0; i<end; i++){
    var buffer = new Buffer( new Uint8Array(picarray[i]) );
    all.write(buffer);
}
all.end();