views:

192

answers:

1

I had uploaded images to mongoDB using GridFS. I want to retrieve an image from the db and display it in the browser. How to do this with the help of Sinatra and HAML.

+1  A: 

This Sinatra app uses the old GridStore API, but the principle still applies. Create a separate URL for images, set the content type, and call #read on the Grid method:

http://github.com/banker/mongodb_examples/blob/master/tweets/sinatra_tweets.rb

Kyle Banker
Actually I'm using GridFileSystem, since GridStore API is deprecated. In GridFileSystem, when I say, read, some byte codes are getting displayed in the browser. How to make the image to appear on the browser?
Prakash
It doesn't matter which API you're using. You need to serve the image with the proper content type. Note how that's done in the example just provided. Note also how I display the image in my html: http://github.com/banker/mongodb_examples/blob/master/tweets/views/tweets.erb
Kyle Banker
This is exactly what I was looking for. Setting the content-type fixed everything for me - thanks!
funkymunky