简体   繁体   English

从jquery webapp将图像上传到google app引擎

[英]upload image to google app engine from jquery webapp

I am attempting to build my own social network as a project to teach myself jQuery and the Google app engine Python API. 我正在尝试建立一个自己的社交网络作为一个项目来自学jQuery和Google应用程序引擎Python API。

I am currently trying to work out how I can upload an image to the app engine's data-store to server as the profile picture. 我目前正在尝试找出如何将图像作为个人资料图片上传到应用程序引擎的数据存储区并发送到服务器的服务器。

I was wondering if someone could give me a quick demonstration to show me how to do this, I've worked out I need to use the ndb.BlobProperty but beyond that i haven't a clue. 我想知道是否有人可以给我快速演示一下如何做到这一点,我已经计算出需要使用ndb.BlobProperty但是除此之外,我没有任何线索。

If it helps, here is the User class from my server-side: 如果有帮助,这是服务器端的User类:

class User(ndb.Model):
    # Because we will use username as an ID/key, no need to 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
        }
        return json.encode(jsondata)

any help would be greatly appreciated :) 任何帮助将不胜感激 :)

One way to do this is to upload to Blob store and then use the googles Images API to serve it. 一种方法是上传到Blob存储区,然后使用googles Images API进行投放。 Try the link. 尝试链接。 Google has given step by step instruction on how to upload and serve it. Google已逐步说明如何上传和提供该服务。

Below is another way on how to use it, A slight difference from the Google doc is that I'm associating just the ID and the serving url to the User. 下面是使用它的另一种方法,与Google文档略有不同的是,我仅将ID和服务网址与用户相关联。

class User(ndb.Model):
.......
profilePicture = ndb.StringProperty(repeated=True) #rather ndb.BlobProperty()
surname = ndb.StringProperty(required=True)
.....

You will have a form that will be used to signup. 您将拥有一个用于注册的表格。 Below is a 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 your application file you should handle the "/signUp" request. 在您的应用程序文件中,您应该处理“ / signUp”请求。 The class will take "blobstore_handlers.BlobstoreUploadHandler" as a parameter rather "webapp2.RequestHandler" 该类将使用“ blobstore_handlers.BlobstoreUploadHandler”作为参数,而不是“ webapp2.RequestHandler”

from google.appengine.ext import blobstore
from google.appengine.api import images
from 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 the above code you'll have a imageData array that will have the url as the first parameter and the key as second. 在上面的代码中,您将有一个imageData数组,该数组将url作为第一个参数,并将键作为第二个参数。 you can use the url to serve images in webpages, usually in a img tag 您可以使用url在网页中提供图像,通常是在img标签中

<img src=the_generated_url>

Save this array as the "profilePicture" property. 将此数组另存为“ profilePicture”属性。 you can use the blob key (the second element in the array) to delete the image if needed. 您可以根据需要使用Blob键(数组中的第二个元素)删除图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM