Wednesday 24 May 2017

How to base64 a buffer in javascript, nodes

I am downloading a document from an external and I am getting the data as a buffer . Now I want to base encode it with base64..

Here is how the data looks with 'JSON.stringify' {"type":"Buffer","data":[80,75,3,4,20,0,0,.....]

I am using node v4.5.0, javascript

This is my rest call to dropbox

function _downloadAttachment(fromUrl, withHeaders, asFilename, callback) {
  request({
    uri: fromUrl,
    method: 'GET',
    encoding: null, // forces the request to respond with a Buffer
    headers: withHeaders
  }, function (error, response, body) {
    if (error) {
      log.info(util.format('Failed to download %s from %s.', asFilename, fromUrl));
      callback(error);
    } else {
      var simplifiedResponse = {
        contentHeaders: {
          'Content-Disposition': response.headers['content-disposition'],
          'Content-Type': response.headers['content-type']
        },
        data: body,
        filename: asFilename
      };

     log.debug(JSON.stringify(simplifiedResponse.data));
      callback(null, simplifiedResponse);
    }
  });
}

How do I base64 encode the data?



via user2570135

No comments:

Post a Comment