0

I keep my images in the DB as blobs:

class MyClass(db.Model):
    icon=db.BlobProperty()

Now, I want to send the blob to my HTML like this : lets say I have myClass as an instance of MyClass

result = """<div img_attr=%s> Bla Bla </div>""" % (myClass.icon)

Some how it doesn't work. Do you have any idea?

rcs
  • 67,191
  • 22
  • 172
  • 153
Omri
  • 11
  • 2

3 Answers3

1

You cannot just dump raw image data into your html page. You need to do this in two pieces:

In your html, you need to refer to an image file:

result = "<div>"
         "<img src='{0}' />"
         "</div>"
         .format(MYSITE + MYIMAGEDIR + myClass.name)

Your browser reads the html page, finds out you want to include an image, and goes looking for the image file to include - so it asks your site for something like http://www.myexample.com/images/icon01.jpg

Now, separately, you respond to this second request with the image content, as @anand has shown.

Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
  • as the code suggest, its on google application engine and writing files is not allowed on it. You have to store your files in blob store itself. – Ankit Jaiswal Jan 01 '11 at 06:20
0

Your code suggests that you are working on Google application engine with Django.

You just need to query the image in your view and return it as http response.

image = myclassObject.icon

response = HttpResponse(image)

response['Content-Type'] = 'image/jpg'

return response

Ankit Jaiswal
  • 22,859
  • 5
  • 41
  • 64
0

The value stored in the the data store, and returned by appengine with a db.BlobProperty is not the actual bytes of the blob, but rather a BlobKey that is used to reference it. There are two ways you can use that key. You can create a BlobReader to load the bytes of the blob from the BlobStore into your app, or you can craft a response with ServeHandler.send_blob to transfer those bytes to the client.

Doing the second one in Django is a bit of a headache, because ServeHandler doesn't really fit in well with the Django request processing stack. Here's a view that will do it for you without too much trouble:

def get_image_data(request, key, filename):
  "serve original uploaded image"

  # verify the users' ability to get the requested image
  key = urllib.unquote(key)
  img = _load_metadata(request, key)
  blob = img.data;
  blobkey = blob.key()

  # and tell google to serve it
  response = http.HttpResponse(
      content='',
      content_type=blob.content_type)
  response['X-AppEngine-BlobKey'] = str(blobkey)
  return response
SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
  • Wrong. A `db.BlobProperty` stores blob data stored in the datastore entity. You're thinking of `blobstore.BlobReferenceProperty`. – Nick Johnson Sep 19 '11 at 04:15