【问题标题】:upload image to google app engine from jquery webapp从 jquery webapp 将图像上传到谷歌应用引擎
【发布时间】:2014-10-20 12:49:29
【问题描述】:

我正在尝试构建自己的社交网络作为一个项目来自学 jQuery 和 Google 应用引擎 Python API。

我目前正在尝试研究如何将图像上传到应用引擎的数据存储到服务器作为个人资料图片。

我想知道是否有人可以给我一个快速演示来告诉我如何做到这一点,我已经确定我需要使用ndb.BlobProperty,但除此之外我没有任何线索。

如果有帮助,这里是我服务器端的 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)

任何帮助将不胜感激:)

【问题讨论】:

  • 我会试一试,但这个例子是在服务器端创建页面:我的 html 文件都是独立的,所以我可能会感到困惑哈哈
  • 在程序中保存并发送到响应的 HTML 页面与发送到响应的文件之间没有区别。但是将它们保存为文件,这是要走的路,内联 HTML 仅适用于示例。

标签: jquery python html google-app-engine jquery-mobile


【解决方案1】:

执行此操作的一种方法是上传到 Blob 存储,然后使用谷歌 Images API 提供服务。试试链接。谷歌已经给出了关于如何上传和提供它的分步说明。

下面是关于如何使用它的另一种方法,与 Google 文档的细微差别是我仅将 ID 和服务 url 与用户相关联。

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

您将获得一个用于注册的表单。下面是一个例子

<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>

在您的应用程序文件中,您应该处理“/signUp”请求。该类将“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")

在上面的代码中,您将拥有一个 imageData 数组,该数组将 url 作为第一个参数,将键作为第二个参数。您可以使用 url 在网页中提供图片,通常在 img 标签中

<img src=the_generated_url>

将此数组保存为“profilePicture”属性。如果需要,您可以使用 blob 键(数组中的第二个元素)删除图像。

【讨论】:

  • 我使用您提供的代码尝试了这个答案,我收到了这个错误:XMLHttpRequest cannot load file:///ppupload.跨源请求仅支持协议方案:http、data、chrome-extension、https、chrome-extension-resource。
  • 您在尝试上传文件时是否遇到此问题?如果您可以正确获取图片服务 URL,它将采用以下格式:your_app_id.appspot.com/randomStringImageId,如果您将其粘贴到浏览器中,您应该会看到该图片。
  • 当我尝试上传图片时,我明白了
  • 这是一个跨域问题。但是,如果您使用的是同一个域,则不应该得到这个。但是,要允许跨域,您需要配置 app.yaml 文件。 “CORS 支持”Google Doc 下的说明。如果您仍然有问题,最好分享代码 sn-ps
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-27
  • 2012-06-11
  • 1970-01-01
  • 2023-03-25
  • 2017-05-03
  • 1970-01-01
相关资源
最近更新 更多