Google App Engine(Python) - 上传文件(图片) [英] Google App Engine (Python) - Uploading a file (image)
问题描述
我希望用户能够将图片上传到Google App Engine。我有以下(Python):
$ p $ class ImageData(ndb.Model):
name = ndb.StringProperty( indexed = False)
image = ndb.BlobProperty()
信息由用户提交使用表单(HTML):
< form name =inputaction =/ registermethod =post >
名称:< input type =textname =name>
图片:< input type =filename =image>
< / form>
然后处理:
<$ p
imagedata = ImageData(parent = image_key(image_name))
imagedata.name = class AddProduct(webapp2.RequestHandler):
def post(self) self.request.get('name')
imagedata.image = self.request.get('image')
imagedata.put()
然而,当我尝试上传图片时,让我们说Book.png,我得到错误:
BadValueError :期望的str,得到了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:'模块'对象没有属性'Blob'
提前致谢
<$ c $ c> imagedata.image = self.request.get('image')
p>
imagedata.image = str(self.request.get('image'))
您的表单还需要enctype =multipart / form-data
< form name =inputaction =/ registermethod =postenctype =multipart / form-data>
I want the user to be able to upload images to Google App Engine. I have the following (Python):
class ImageData(ndb.Model):
name = ndb.StringProperty(indexed=False)
image = ndb.BlobProperty()
Information is submitted by the user using a form (HTML):
<form name = "input" action = "/register" method = "post">
name: <input type = "text" name = "name">
image: <input type = "file" name = "image">
</form>
Which is then processed by:
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()
However, when I try to upload an image, lets say "Book.png", I get the error:
BadValueError: Expected str, got u'Book.png'
Any idea what is going on? I have been working with GAE for quite some time, but this is the first time I had to use blobs.
I used this link: https://developers.google.com/appengine/docs/python/images/usingimages
which uses db, not ndb.
I also tried storing the image in a variable first like in the link:
storedInfo = self.request.get('image')
and then storing it:
imagedata.image = ndb.Blob(storedInfo)
Which ALSO gives me an error:
AttributeError: 'module' object has no attribute 'Blob'
Thanks in advance.
Had the same prob.
just replace
imagedata.image = self.request.get('image')
with:
imagedata.image = str(self.request.get('image'))
also your form needs to have enctype="multipart/form-data
<form name = "input" action = "/register" method = "post" enctype="multipart/form-data">
这篇关于Google App Engine(Python) - 上传文件(图片)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!