Wednesday, 15 July 2015

python - upload image to google app engine from jquery webapp -



python - upload image to google app engine from jquery webapp -

i attempting build own social network project teach myself jquery , google app engine python api.

i trying work out how can upload image app engine's data-store server profile picture.

i wondering if give me quick demonstration show me how this, i've worked out need utilize ndb.blobproperty beyond haven't clue.

if helps, here user class server-side:

class user(ndb.model): # because utilize username id/key, no need define it. profilepicture = ndb.blobproperty() surname = ndb.stringproperty(required=true) email = ndb.stringproperty(required=true) password = ndb.stringproperty(required=true) banned = ndb.booleanproperty(required=true) rank = ndb.integerproperty(required=true) strikes = ndb.integerproperty(required=true) def tojson(self): jsondata = { "username" : self.key.id(), "forename" : self.forename, "surname" : self.surname, "email" : self.email, "password" : self.password, "banned" : self.banned, "rank" : self.rank } homecoming json.encode(jsondata)

any help appreciated :)

one way upload blob store , utilize googles images api serve it. seek link. google has given step step instruction on how upload , serve it.

below way on how utilize it, slight difference google doc i'm associating id , serving url user.

class user(ndb.model): ....... profilepicture = ndb.stringproperty(repeated=true) #rather ndb.blobproperty() surname = ndb.stringproperty(required=true) .....

you have form used signup. below example

<form action="/signup" enctype="multipart/form-data" method="post"> <input name="name"></input> <label>avatar:</label> <input type="file" name="img"/> <input type="submit" value="create account"></div> </form>

in application file should handle "/signup" request. class take "blobstore_handlers.blobstoreuploadhandler" parameter rather "webapp2.requesthandler"

from google.appengine.ext import blobstore google.appengine.api import images google.appengine.ext.webapp import blobstore_handlers class newuser(blobstore_handlers.blobstoreuploadhandler): def post(self): try: image = self.get_uploads('img') url = images.get_serving_url(image.key(),400) print ("image url %s" %url) imagedata = [url, image.key()] except: print ("something went wrong")

in above code you'll have imagedata array have url first parameter , key second. can utilize url serve images in webpages, in img tag

<img src=the_generated_url>

save array "profilepicture" property. can utilize blob key (the sec element in array) delete image if needed.

jquery python html5 google-app-engine jquery-mobile

No comments:

Post a Comment