4

I need to create base64 string that I need to send to a third party API. I have the stream and buffer. Form stream I am able to create an image so there is no way the stream is corrupted. Here are the two variables:

var newJpeg = new Buffer(newData, "binary");
var fs = require('fs');
let Duplex = require('stream').Duplex;
let _updatedFileStream = new Duplex();
_updatedFileStream.push(newJpeg);
_updatedFileStream.push(null); 

No matter whatever I try, I can not convert either of them in base64 string.

_updatedFileStream.toString('base64');
Buffer(newJpeg, 'base64');
Buffer(newData, 'base64');

None of the above works. Sometimes I get Uint8Array[arraySize] or Gibberish string. What am I doing wrong?

asked Sep 5, 2017 at 12:03

1 Answer 1

8

Example using promises (but could easily be adapted to other approaches):

return new Promise((resolve, reject) => {
 let buffers = [];
 let myStream = <...>;
 myStream.on('data', (chunk) => { buffers.push(chunk); });
 myStream.once('end', () => {
 let buffer = Buffer.concat(buffers);
 resolve(buffer.toString('base64'));
 });
 myStream.once('error', (err) => {
 reject(err);
 });
});
answered Oct 5, 2017 at 11:25

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.