Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting url for an attachment

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?"
      };
    })
  };
};
like image 987
Kuepper Avatar asked Jul 23 '11 22:07

Kuepper


People also ask

How do I find the URL of a WordPress file?

Step 1 - In the WordPress backend, goto left menu Media -> Library, find the media file, then click the Edit link. Step 2 - In the Edit Media page, copy the URL from the right side File URL input box.

How do I find the URL of a WordPress image?

Finding Your WordPress Image URLs from the FrontendOpen a page or post where the image is published and locate the image. After that, right-click on the image, and select 'Open image in new tab' option. Once the image opens in the new tab, look at the URL in the address bar. That is your WordPress image URL.

How do I find attachments in WordPress?

If the attachment is an image, the function returns an image at the specified size. For other attachments, the function returns a media icon if the $icon parameter is set to true. To get attachment IDs dynamically in a template, you can use get_posts( array( 'post_type' => 'attachment' ) ) , etc.

What is _wp_attached_file?

What is _wp_attached_file? The function works by getting the single post meta name, named '_wp_attached_file' and returning it. This is a convenience function to prevent looking up the meta name and provide a mechanism for sending the attached filename through a filter.


1 Answers

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;
like image 153
Jørgen Avatar answered Oct 02 '22 00:10

Jørgen