谷歌应用程序引擎&x2B;python:上载到blobstore会导致编码错误
我尝试使用以下HTML表单将Blob上传到Google App Engine的blobstore:谷歌应用程序引擎&x2B;python:上载到blobstore会导致编码错误,python,google-app-engine,blobstore,Python,Google App Engine,Blobstore,我尝试使用以下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: <i
<!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>
通常,
名称
字段将填充非英语字符(例如,中文)。以上程序在我的本地SDK上运行良好。但是,当程序在Google App Engine上运行时,名称
的编码不正确。那么问题出在哪里呢?您不需要在meta标记参数周围加引号:
?另外,请尝试:
。另外,请确保以UTF-8编码保存模板的文本文档。刚刚发现这是多年来的老错误,请参阅。有两种解决方案:
(1) 将以下语句添加到app.yaml中:
libraries:
- name: webob
version: "1.2.3"
(2) 添加包含以下内容的文件appengine_config.yaml:
# -*- coding: utf-8 -*-
from webob import multidict
def from_fieldstorage(cls, fs):
"""Create a dict from a cgi.FieldStorage instance.
See this for more details:
http://code.google.com/p/googleappengine/issues/detail?id=2749
"""
import base64
import quopri
obj = cls()
if fs.list:
# fs.list can be None when there's nothing to parse
for field in fs.list:
if field.filename:
obj.add(field.name, field)
else:
# first, set a common charset to utf-8.
common_charset = 'utf-8'
# second, check Content-Transfer-Encoding and decode
# the value appropriately
field_value = field.value
transfer_encoding = field.headers.get('Content-Transfer-Encoding', None)
if transfer_encoding == 'base64':
field_value = base64.b64decode(field_value)
if transfer_encoding == 'quoted-printable':
field_value = quopri.decodestring(field_value)
if field.type_options.has_key('charset') and field.type_options['charset'] != common_charset:
# decode with a charset specified in each
# multipart, and then encode it again with a
# charset specified in top level FieldStorage
field_value = field_value.decode(field.type_options['charset']).encode(common_charset)
# TODO: Should we take care of field.name here?
obj.add(field.name, field_value)
return obj
multidict.MultiDict.from_fieldstorage = classmethod(from_fieldstorage)
try:test.name=self.request.get('name')。decode('utf-8')错误消息:
UnicodeEncodeError:'ascii'编解码器无法对位置0:ordinal not in range(128)
中的字符u'\u6211'进行编码。您可以尝试在不使用上载url和重定向的情况下进行上载,以查找编码问题。请看一下gcs_upload.py中的要点:谢谢。但HTML5允许不使用引用语。因此,为了提高效率,我认为最好减小HTML文件的大小。
# -*- coding: utf-8 -*-
from webob import multidict
def from_fieldstorage(cls, fs):
"""Create a dict from a cgi.FieldStorage instance.
See this for more details:
http://code.google.com/p/googleappengine/issues/detail?id=2749
"""
import base64
import quopri
obj = cls()
if fs.list:
# fs.list can be None when there's nothing to parse
for field in fs.list:
if field.filename:
obj.add(field.name, field)
else:
# first, set a common charset to utf-8.
common_charset = 'utf-8'
# second, check Content-Transfer-Encoding and decode
# the value appropriately
field_value = field.value
transfer_encoding = field.headers.get('Content-Transfer-Encoding', None)
if transfer_encoding == 'base64':
field_value = base64.b64decode(field_value)
if transfer_encoding == 'quoted-printable':
field_value = quopri.decodestring(field_value)
if field.type_options.has_key('charset') and field.type_options['charset'] != common_charset:
# decode with a charset specified in each
# multipart, and then encode it again with a
# charset specified in top level FieldStorage
field_value = field_value.decode(field.type_options['charset']).encode(common_charset)
# TODO: Should we take care of field.name here?
obj.add(field.name, field_value)
return obj
multidict.MultiDict.from_fieldstorage = classmethod(from_fieldstorage)