【发布时间】:2013-08-25 21:53:01
【问题描述】:
我希望用户能够将图像上传到 Google App Engine。我有以下(Python):
class ImageData(ndb.Model):
name = ndb.StringProperty(indexed=False)
image = ndb.BlobProperty()
用户使用表单 (HTML) 提交信息:
<form name = "input" action = "/register" method = "post">
name: <input type = "text" name = "name">
image: <input type = "file" name = "image">
</form>
然后由以下人员处理:
class AddProduct(webapp2.RequestHandler):
def post(self):
imagedata = ImageData(parent=image_key(image_name))
imagedata.name = self.request.get('name')
imagedata.image = self.request.get('image')
imagedata.put()
但是,当我尝试上传图片时,可以说“Book.png”,我收到错误消息:
BadValueError: Expected str, got u'Book.png'
知道发生了什么吗?我使用 GAE 已经有一段时间了,但这是我第一次不得不使用 blob。
我使用了这个链接:https://developers.google.com/appengine/docs/python/images/usingimages
它使用 db,而不是 ndb。
我还尝试先将图像存储在变量中,如链接中:
storedInfo = self.request.get('image')
然后存储它:
imagedata.image = ndb.Blob(storedInfo)
这也给了我一个错误:
AttributeError: 'module' object has no attribute 'Blob'
提前致谢。
【问题讨论】:
-
该错误告诉您您正在尝试将 Unicode 对象设置为 blob 的值,在这种情况下,它似乎是文件名,而不是文件数据本身。 (我不确定如何在 webapp2 中将原始数据作为 str 获取,所以只是作为评论发布)
-
您没有使用 blobstore api 而不是数据存储的任何原因? developers.google.com/appengine/docs/python/blobstore/…
-
谢谢你,Wooble,但你建议我做什么? Faisal,我没有使用它,因为它需要 db(我正在使用 ndb)和 webapp(我正在使用 webapp2)。
-
我不使用 webapp2(或 webapp),但粗略检查文档表明您做错了。上传的文件通常会通过 cgi.FieldStorage 对象访问 - 请参阅文档webapp-improved.appspot.com/guide/request.html。此外,您可能需要将编码正常设置为 multipart/form-data 参见文档 - w3.org/TR/html401/interact/forms.html#h-17.13.4
-
您能否提供该示例的链接,以便其他有同样问题的人完成?
标签: python image google-app-engine upload app-engine-ndb