I'm trying to use nano to insert a document with multiple image attachments. The following test case runs without failure:
var dbName = 'testdb';
var encoding = 'base64';
var fs = require('fs');
var nano = require('nano')('http://localhost:5984');
nano.db.create(dbName);
var db = nano.use(dbName);
var attach1 = {
name: 'image_1',
data: fs.readFileSync('test_image.jpg').toString(encoding),
content_type: 'image/jpeg'
};
var attach2 = {
name: 'image_2',
data: fs.readFileSync('test_image_2.jpg').toString(encoding),
content_type: 'image/jpeg'
};
var doc = {
_id: 'test_id',
html: fs.readFileSync('test_html.html').toString()
};
db.multipart.insert(doc, [attach1, attach2], doc._id, function(err){
if (err)
console.log('failed: ' + err);
else
console.log('succeeded');
});
However, when I try to view the images using futon, they seem broken, and if I download the images and try to open them, I'm told "Error interpreting JPEG image file (Not a JPEG file: starts with 0x2f 0x39)".
The files have roughly the correct size (slightly bigger on the server, but not by much), so I'm guessing there's some formatting error. That said, I've tried binary and utf8 as encoding values, and I'm not sure what else to do.
The data should be a Buffer, not base64 encoded strings, you can just use the result of fs.readFileSync
:
var attach1 = {
name: 'image_1',
data: fs.readFileSync('test_image.jpg').toString(encoding),
content_type: 'image/pjpeg'
};
Note that nano upto 5.11.2 had a bug with unicode chars when using strings in multipart inserts, see https://github.com/dscape/nano/pull/225.