所以我正在使用 Google App Engine 作为应用程序,我需要将图片从 GAE 传递到 android 客户端。为了做到这一点,我虽然将图片转换为字符串并将其作为 json 发送......(我发现这是一个不错的想法:使用 Python Base64 压缩 GAE Blob 图像?)。
问题是我很难在 Base64 中进行转换。我已经尝试过这种方法(在 AppEngine 上对上传的二进制数据进行 Base64 编码),但它对我不起作用,因为我收到错误:
File "/base/data/home/apps/s~testehan/1.366670408737847123/main.py", line 462, in post
img = Imagine( name=fileupload.filename, data=fileupload.file.read() )
File "/python27_runtime/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 970, in __init__
prop.__set__(self, value)
File "/python27_runtime/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 614, in __set__
value = self.validate(value)
File "/python27_runtime/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 2806, in validate
value = super(UnindexedProperty, self).validate(value)
File "/python27_runtime/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 641, in validate
raise BadValueError('Property %s is required' % self.name)
BadValueError: Property data is required
原因是当我在日志中显示 upload_file.file.read() 时,我得到了这个:
Content-Type: image/jpeg
Content-Length: 36187
X-AppEngine-Upload-Creation: 2013-04-14 08:35:46.123191
Content-MD5: MDYzNWE2Y2FkMzFlMGVhNzgwNmEwNzhiOGNkZTdhNTI=
Content-Disposition: form-data; name=img; filename="blabla.jpg"
所以我的问题是..我如何阅读这张图片并将其存储为文本属性,以便我能够传递它?
谢谢
编辑:后处理程序
class Imagine(db.Model):
name = db.StringProperty(required=True)
data = db.TextProperty(required=True)
created = db.DateTimeProperty(auto_now_add=True)
class PostImage(UserHandler):
def post(self, restaurant, category):
logging.info("ImagestoreHandler#post %s", self.request.path)
name= self.request.get('name')
description= self.request.get('description')
sizes = int(self.request.get('sizes'))
fileupload = self.request.POST.get("img",None)
if fileupload is None : return self.error(400)
content_type = fileupload.type or getContentType( fileupload.filename )
if content_type is None:
self.error(400)
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write( "Unsupported image type: " + fileupload.filename )
return
logging.debug( "File upload: %s, mime type: %s", fileupload.filename, content_type )
try:
logging.info('citesc fisierul : ' + fileupload.file.read())
img = Imagine( name=fileupload.filename, data=fileupload.file.read() )
img.put()
(img_name, img_url) = self._store_image( fileupload.filename, fileupload.file, content_type )
self.response.headers['Location'] = img_url
ex=None
except Exception, err:
logging.exception( "Error while storing image" )
self.error(400)
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write("Error uploading image: " + str(err))
return
#self.redirect(urlBase % img.key() ) #dummy redirect is acceptable for non-AJAX clients,
# location header should be acceptable for true REST clients, however AJAX requests
# might not be able to access the location header so we'll write a 200 response with
# the new URL in the response body:
acceptType = self.request.accept.best_match( listRenderers.keys() )
out = self.response.out
if acceptType == 'application/json':
self.response.headers['Content-Type'] = 'application/json'
out.write( '{"name":"%s","href":"%s"}' % ( img_name, img_url ) )
elif re.search( 'html|xml', acceptType ):
self.response.headers['Content-Type'] = 'text/html'
out.write( '<a href="%s">%s</a>' % ( img_url, img_name) )
def _store_image(self, name, file, content_type):
"""POST handler delegates to this method for actual image storage; as
a result, alternate implementation may easily override the storage
mechanism without rewriting the same content-type handling.
This method returns a tuple of file name and image URL."""
img_enc = base64.b64encode(file.read())
img_enc_struct = "data:%s;base64,%s" % (content_type, img_enc)
logging.info('file name is '+name)
logging.info('file is '+file.read())
img = Imagine( name=name, data=file.read() )
img.put()
return ( str(img.name), img.key() )
形式 :
<form action="{{ upload_url }}" method="POST" enctype="multipart/form-data">
<input name="name" placeholder="Product name" type="text" />
<input type="file" name="img"/>
<br />
<input type="submit" value="Submit product!" />
</form>