そのため、アプリに Google App Engine を使用しています。GAE から Android クライアントに画像を渡す必要があります。これを行うには、画像をストリグに変換してjsonとして送信します...(ここでこれは良いアイデアであることがわかりました:Python Base64を使用してGAE Blob画像を圧縮しますか?)。
問題は、Base64で変換を行うのに苦労していることです..私はこのアプローチを試しました(Base64はAppEngineでアップロードされたバイナリデータをエンコードします)が、エラーが発生するためうまくいきません:
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>