1

I'm trying to insert a binary attachment to CouchDB with nano. I have a JPG in data returned by http.request.

I save it with nano as follows

db.attachment.insert( id, 'content', self._data, contentType, {rev: rev}, function(err, body) { 
        callback();
});

but when I try view it though a web browser the image is broken.

The file is full of UTF-8 escape characters which is visible when I pull it with CURL:

$ curl http://127.0.0.1:5984/web-crawler/doc-test.jpg/content
"ÿØÿà\u0000\u0010JFIF\u0000\u0001\u0001\u0001\u0000H\u0000H\u0000\u0000ÿâ\fXICC_PROFILE\u0000\u0001\u0001\u0000\u0000\fHLino\u0002\u0010\u0000\u0000mntrRGB XYZ \u0007Î\u0000\u0002\u0000\t\u0000\u0006\u00001\u0000\u0000acspMSFT\u0000\u0000\u0000\u0000IEC sRGB\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000öÖ\u0000\u0001\u0000\u0000\u0000\u0000Ó-HP  \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\

Content is not corrupted because if I save it to a file I can see the image.

var fs = require('fs');
fs.writeFile('logo.jpg', data, 'binary', function(err){
  if (err) throw err
  console.log('File saved.')
});

What is the right way to do it?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Lukasz Kujawa
  • 3,026
  • 1
  • 28
  • 43

1 Answers1

3

Ok, this question can be close. The answer to my problem is Buffer:

db.attachment.insert( id, 'content', new Buffer(self._data, "binary"), contentType, {rev: rev}, function(err, body) { 
        callback();
});
Lukasz Kujawa
  • 3,026
  • 1
  • 28
  • 43