Skip to main content
 首页 » 编程设计

python之谷歌应用引擎 + python : uploading to blobstore causes wrong encoding

2024年11月01日10grandyang

我尝试使用以下 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 编码不正确。那有什么问题呢?

请您参考如下方法:

您不必在元标记参数周围加上引号:<meta charset="UTF-8"> ?另外,尝试:<meta http-equiv="content-type" content="text/html; charset=utf-8" /> .并且,确保您以 UTF-8 编码保存模板的文本文档。