Base64 对 AppEngine 上上传的数据进行二进制编码
我一直在尝试对来自用户(在本例中是受信任的管理员)的图像数据进行 Base64 编码,以便尽可能跳过对 BlobStore 的调用。每次我尝试对其进行编码时,都会收到一条错误消息:
Error uploading image: 'ascii' codec can't decode byte 0x89 in position 0: ordinal not in range(128)
我用 google 搜索了该错误(不太重要的部分),发现它可能与 Unicode 有关(?)。模板部分只是一个基本的上传表单,而处理程序包含以下代码:
def post(self,id):
logging.info("ImagestoreHandler#post %s", self.request.path)
fileupload = self.request.POST.get("file",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:
(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)
img = Image( name=name, data=img_enc_struct )
img.put()
logging.info("Saved image to key %s", img.key() )
return ( str(img.name), img.key() )
我的图像模型:
from google.appengine.ext import db
class Image(db.Model):
name = db.StringProperty(required=True)
data = db.TextProperty(required=True)
created = db.DateTimeProperty(auto_now_add=True)
owner = db.UserProperty(auto_current_user_add=True)
非常感谢任何帮助。此代码减去 _store_image 中的图像编码,来自 gvdent 此处 的 blooger 分支。
I've been trying to Base64 encode image data from the user (in this case a trusted admin) in order to skip as many calls to the BlobStore as I possibly can. Every time I attempt to encode it, I recieve an error saying:
Error uploading image: 'ascii' codec can't decode byte 0x89 in position 0: ordinal not in range(128)
I've googled the error (the less significant parts) and found that it may have something to do with Unicode (?). The template portion is just a basic upload form, while the handler contains the following code:
def post(self,id):
logging.info("ImagestoreHandler#post %s", self.request.path)
fileupload = self.request.POST.get("file",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:
(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)
img = Image( name=name, data=img_enc_struct )
img.put()
logging.info("Saved image to key %s", img.key() )
return ( str(img.name), img.key() )
My image model:
from google.appengine.ext import db
class Image(db.Model):
name = db.StringProperty(required=True)
data = db.TextProperty(required=True)
created = db.DateTimeProperty(auto_now_add=True)
owner = db.UserProperty(auto_current_user_add=True)
Any help is greatly appreciated. This code, minus my image encoding in _store_image, comes from the blooger branch of gvdent here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的商店图像代码可能是这样的......
对二进制数据进行base64encode可能会增加数据本身的大小并增加CPU编码和解码时间。
并且 Blobstore 使用与数据存储相同的存储结构,因此它只是使更容易
使用文件上传商店下载。
your store image code could be like this....
doing base64encode of binary data may increase the size of data itself and increase the cpu encoding and decoding time.
and Blobstore uses the same storage sturcuture as datastore, so it is just making easier to
use file upload store download.