Google App Engine (Python) - 上传文件(图片) [英] Google App Engine (Python) - Uploading a file (image)

查看:44
本文介绍了Google App Engine (Python) - 上传文件(图片)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望用户能够将图像上传到 Google App Engine.我有以下(Python):

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

用户使用表单 (HTML) 提交信息:

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>

然后由以下处理:

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'

However, when I try to upload an image, lets say "Book.png", I get the error: BadValueError: Expected str, got u'Book.png'

知道发生了什么吗?我使用 GAE 已经有一段时间了,但这是我第一次不得不使用 blob.

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.

我使用了这个链接:https://developers.google.com/appengine/docs/python/图片/使用图片它使用 db,而不是 ndb.我还尝试首先将图像存储在链接中的变量中:storedInfo = self.request.get('image')然后存储它:imagedata.image = ndb.Blob(storedInfo)这也给了我一个错误:AttributeError: 'module' 对象没有属性 'Blob'提前致谢.

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.

推荐答案

有同样的问题.

直接替换

imagedata.image = self.request.get('image')

与:

imagedata.image = str(self.request.get('image'))

你的表单也需要有 enctype="multipart/form-data

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆