Getting url for an attachment
Asked Answered
S

1

6

I'm using CouchApp to build an easy web application that allows to upload and manage pictures. The actual image file is stored as attachment to the doc like show below.

{
  "_id":"09fe82d75a26f9aa5e722d6b220180d2",
  "_rev":"2-5797b822c83b9d41545139caa592f611",
  "data":"some additional fields with info about the image",
  "_attachments":
  {
    "foo.jpg":
    {
      "stub":true,
      "content_type":"image/jpeg",
      "length":23721
    }
  }
}

But for integrating the image in html i need the url to the attachment. How do i get this url?

I'm using evently and mustache for generating the web pages. Below is the data.js for reading the data:

function(data) {
  return {
    items : data.rows.map(function(r) {
      return {
        id : r.value._id,
        rev : r.value._rev,
        title : r.value.description,
        url : "how am i supposed to do this?"
      };
    })
  };
};
Schoenfeld answered 23/7, 2011 at 22:32 Comment(1)
Have you read this wiki.apache.org/couchdb/HTTP_Document_API#Attachments ?Lianeliang
B
14

The URL to the attachment would be http://domain/database/09fe82d75a26f9aa5e722d6b220180d2/foo.jpg

If your filenames are dynamic, you would have to iterate the _attachments object and fetch the keys on your way - that's where your filename would be.

for(var filename in r.value._attachments){break;}
// ...
url : '<database>/' + r.value._id +'/'+filename;
Bray answered 28/7, 2011 at 7:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.