display image attachments from a CouchDB document on my site

283 views
Skip to first unread message

dpetrovics

unread,
Mar 7, 2012, 3:43:12 PM3/7/12
to clj-...@googlegroups.com
Hey guys,
I am developing a web application using Noir and CouchDB. I store some attachments (images) in my documents on CouchDB. There is a form on my site with a file input that users can use to put images in the database, and that works well (I am using Asafa- Clutch by the way).
I want to have a page on the site where I can display their images that they have uploaded. So, to get the attachment back, I call (clutch/get-attachment db document filename) and I receive an HTTPInputStream. In clutch he mentions that I can use clojure.contrib.io/to-byte-array to convert this to a byte array. I am new to Noir, and my question is, how I can take either the input stream or byte array of the image and have the image displayed on a page?
I am thinking something like: 

(defpage "/:user/getphoto" {:keys [user] :as m}
  ;;shared/page is a function that uses a 'deftemplate' 
  ;;user/get-photo returns an HTTPInputStream of the photo from the db
  (shared/page {:main (content (user/get-photo user "main photo 2"))
                :active-section "Photos"
                :sources ["/cljs/bootstrap-tab.js"]})
  )

The above of course doesnt work, because content cant take an HTTPInputStream. 
Any ideas how I should do this?

Thanks
-David

Mark Rathwell

unread,
Mar 7, 2012, 3:56:37 PM3/7/12
to clj-...@googlegroups.com
In your template, in the img tag set the image source as a url that
returns the image (/:user/photo/:id would just return an image and
another page returns the templated html), unless I'm misunderstanding
what you are trying to do.

dpetrovics

unread,
Mar 10, 2012, 12:26:57 AM3/10/12
to clj-noir
Thanks Mark,
Works now. I didn't realize I could just put the HTTPInputStream in a
defpage and the image would show up. Makes sense though!
Reply all
Reply to author
Forward
0 new messages