I have a mobile app based on PhoneGap and Couchbase server. Now I want to extract some information for a website and, in particular, show images stored in the buckets from the mobile app.
How can I retrieve such images?
Using CouchNode:
db.get(req.params.id, function(err, result) {
if (err) {
// Failed to retrieve key
res.json(err)
} else {
var doc = result.value;
res.json(doc)
}
});
I only get this:
{
"_attachments": {
"image": {
"content_type": "image/jpeg",
"digest": "sha1-paOMVTB9eVTf5xAt9pYUfiMc/ss=",
"length": 897725,
"revpos": 3,
"stub": true
}
},
"_sync": {
"rev": "3-749b8a86a63e7b3661c568aac5e9ab96",
etc...
Then what? How to show the image or get the URL to the image?
Thanks
Luca
Hi luca,
Can you show use a sample of code when you record this image into the bucket ?
The image is stored by the mobile apps, not by the Node app. So it goes through sync_gateway and gets stored.
Anyway the code in Cordova/JS is this:
var reader = new FileReader();
reader.onloadend = function(evt) {
var imageStr = evt.target._result
var content_type=imageStr.substring(imageStr.lastIndexOf("data:")+5,imageStr.lastIndexOf(";base64,"));
var data=imageStr.substring(imageStr.lastIndexOf(";base64,")+8);
var attachment = {
content_type : content_type,
data : data
}
var doc = angular.copy(base_doc)
doc._attachments.image = attachment
CouchBase.saveItem(doc).then(function(saveImage) {
console.log("saveImage",saveImage);
}, function(error) {
console.log("saveImage Error",error);
})
}
I am checking and probably the best way to retrieve the image content is through the REST API of sync gateway.
What do you think?
Confirmed. The image can be retrieved through a GET request to the sync_gateway:
Sync_Gateway documentation