How to upload images and storing it in db google app engine

69 views Asked by At

Hello i want to know how to upload a picture from the user and store it in data base using this form ( using google data store)

<form method='post'>
<input type='file' name='img'>
<input type='submit'>

i have made a db model

from google.appengine.ext import db
class Photo(db.model):
     profile_pic = db.BlobProperty()

and when i display it in the web app the name only appears

ex : example.png

i want to know how to upload it using

from google.appengine.ext import db

not

from google.appengine.ext import ndb
1

There are 1 answers

0
Niklas Rosencrantz On BEST ANSWER

You can use the blobstore and a BlobReferenceProperty.

class Image(db.Model): 
    primary_image = blobstore.BlobReferenceProperty()

Then you have an upload handler where you can add image using http post:

class UploadHandler(BaseRequestHandler,
               blobstore_handlers.BlobstoreUploadHandler):
   def post(self):

        for upload in self.get_uploads():
            try:
                img = Image()
                img.primary_image = upload.key()
                img.put()