Getting url for an attachment

风流意气都作罢 提交于 2019-12-06 02:56:34

问题


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?"
      };
    })
  };
};

回答1:


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;


来源:https://stackoverflow.com/questions/6803741/getting-url-for-an-attachment

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!