我试图弄清楚如何使用在以下位置找到的node-CouchDB库将图像插入CouchDB:https://www.npmjs.com/package/node-couchdb
这就是我所做的:
fs.readFile('download.jpeg',(err,data) => {
binary_data = new Buffer(data,'binary');
couch.insertAttachment("node_db",doc_number,"download.jpeg",binary_data,rev_number).then(({data,headers,status}) => {
},err => {
console.log("ERROR"+ err.code);
});
});
结果是CouchDB以如下文档格式存储它:
{
"_id": "2741d6f37d61d6bbdf63df3be5000504","_rev": "22-bfdbe6db35c7d9873a2cc8a38afb2833","_attachments": {
"attachment": {
"content_type": "application/json","revpos": 22,"digest": "md5-on0A+d7045WPI6FyS1ut4g==","length": 22482,"stub": true
}
}
}
//This is what the data looks like in CouchDB using the View Attachment Function through the interface:
{"type":"Buffer","data":[255,216,255,224,16,74,70,73,1,219,132,9,6,7,18,21,19,22,23,24,29,40,32,26,37,33,49,41,43,46,31,51,56,45,55,10,14,13,241,...]
然后我尝试将请求标题中的Content-Type属性更改为“ image / jpeg”,结果是:
{
"_id": "2741d6f37d61d6bbdf63df3be5000504","_rev": "23-cf8c2076b43082fdfe605cad68ef2355","_attachments": {
"attachment": {
"content_type": "image/jpeg","revpos": 23,"digest": "md5-SaekQP37DCCeGX2M8UVeGQ==","stub": true
}
}
}
但是,这仍然会导致在CouchDB界面中无法查看图像(单击“查看附件”)。在这种情况下,图像的大小仅为6,904字节,但存储时的长度约为22k(夸大了CouchDB中的大小),因此我假设我没有将图像的正确表示(编码)传递给CouchDB