【发布时间】:2014-05-30 13:58:28
【问题描述】:
我尝试使用以下 HTML 表单将 blob 上传到 Google App Engine 的 blobstore:
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
</head>
<body>
<form id=upload action={{upload_url}} method=post enctype=multipart/form-data>
Name: <input type=text name=name>
Your photo: <input type=file name=image required=required><br><br>
<input type=submit value=submit>
</form>
</body>
</html>
模板变量{{upload_url}}的值是通过服务器端的upload_url = blobstore.create_upload_url('/upload')获取的。后处理脚本如下:
class Test(ndb.Model):
name = StringProperty()
image = StringProperty()
test = Test()
test.name = self.request.get('name')
image = self.get_uploads('image')[0]
test.image = str(image.key())
test.put()
通常,name 字段将填充非英文字符(例如中文)。上述程序在我的本地 SDK 上运行良好。但是,当程序在 Google App Engine 上运行时,name 的编码不正确。那有什么问题呢?
【问题讨论】:
-
尝试:test.name = self.request.get('name').decode('utf-8')
-
好吧,错误信息:
UnicodeEncodeError: 'ascii' codec can't encode character u'\u6211' in position 0: ordinal not in range(128) -
你可以尝试不带upload_url和redirect的方式上传,找出编码问题。看看这个要点中的 gcs_upload.py:gist.github.com/voscausa/9541133
标签: python google-app-engine blobstore