0

my problem is that i can get the save picture to gridFS even if it's there, i've verified and it show me the piture and its name and the size from the console. here is the code:

conn = Connection()

the classe that saves to the database:

class Profile(tornado.web.RequestHandler):

def post(self):
      self.db = conn["essog"]
      avat = self.request.files['avatar'][0]["body"]
      avctype = self.request.files['avatar'][0]["content_type"]
      nomfich = self.request.files['avatar'][0]["filename"]
      #..operation using PIL to decide to save the picture or not
      self.fs = GridFS(self.db)
      avatar_id = self.fs.put(avat, content_type=avctype, filename=nomfich) #change the   name later to avoid delete using the same name, so generating a different name...
      .....
      user={..., "avatar":avatar_id}
      self.db.users.insert(user)
      self.db.users.save(user)

the class that reads from the database:

class Profil(tornado.web.RequestHandler):

  def get(self):
      self.db = conn["essog"]
      self.fs = GridFS(self.db)
      avatar_id = self.db.users.find_one()["avatar"]
      ...
      avatar = self.fs.get(avatar_id).read()
      self.render("profile.html",..., avatar=avatar)

and in the view (profile.html)

img src="{{avatar}}" />

but nothing is displayed!

Abdelouahab Pp
  • 4,252
  • 11
  • 42
  • 65

2 Answers2

3

Unless you want to use a base64 URI for the source of the image, you should use a url and then create a view for returning the data from that view. If you are using nginx you might be interested in the nginx-gridfs module for better performance.

Ross
  • 17,861
  • 2
  • 55
  • 73
  • sorry, but i dont understand this?! why in the normal case i can only show the image source path and it will be okey, it's not the sace when it's dealing with database? – Abdelouahab Pp Aug 13 '12 at 16:59
  • do i understand that i MUST re-save it to a directory? http://groups.google.com/group/mongodb-user/browse_thread/thread/f68a2f95370105a6 – Abdelouahab Pp Aug 13 '12 at 17:14
  • 1
    No - its just you are outputting the contents of the image in the src of the image - if you do that you must use a data uri. Normally you will provide a URL that will return the contents and headers with the content type data – Ross Aug 13 '12 at 17:20
  • heu...sorry, but am a beginner, and it's the fist time i see this, i dident understood the example given in wikipedia, can you please explain me a simple example using an imaginary "avatar_id" ? – Abdelouahab Pp Aug 13 '12 at 17:25
  • here is another answer that i dont get: http://grokbase.com/t/gg/mongodb-user/126ngn7vsa/using-gridfs-to-store-related-files-in-one-document#20120621bpiwuvwmmvctmoxwyjybyfltsi he said that: "Serializing binary files also increases their size (by about 30% for base64), and needs processing power both inserting and retrieving the file." – Abdelouahab Pp Aug 13 '12 at 17:33
2

The src attribute of an img tag does not (typically) contain the image data itself, but rather the URL of the image. I think you're confusing two separate requests and responses:

  1. HTML page that contains an <img src="..." /> tag:

    class Profil(tornado.web.RequestHandler):
        self.render('profile.html',
            avatar=self.reverse_url('avatar', avatar_id))
    
  2. image itself (which needs a separate handler):

    class Avatar(tornado.web.RequestHandler):
        def get(self, avatar_id):
            avatar = self.fs.get(avatar_id)
            self.set_header('Content-Type', avatar.content_type)
            self.finish(avatar.read())
    
ESV
  • 7,620
  • 4
  • 39
  • 29
  • ah, that's what i've asked this morning after studying the Slock open source project. http://groups.google.com/group/python-tornado/browse_thread/thread/22d65ddcd6698b4e now i got it, thank you :) – Abdelouahab Pp Aug 15 '12 at 17:45