提交多部分/表单数据表单时,应用引擎中的编码问题 [英] Encoding problem in app engine when submitting multipart/form-data forms

查看:107
本文介绍了提交多部分/表单数据表单时,应用引擎中的编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的形式,提交一个图像到blobstore和图像的标题。
这在我的本地devserver工作,但是当我部署我的代码,标题中的非ASCII字母变成乱码与ascii和hex的混合。例如Ísland变为= CDsland。请注意,我使用< meta http-equiv =Content-Typecontent =text / html; charset = utf-8> 标题。 utf-8也适用于我所有的其他形式。只是多部分形式变成乱码。无论如何,这是我的形式:

I have a simple form that submits a image to the blobstore and a title for the image. This works on my local devserver but when I deploy my code, non ascii letters in the title become garbled with some kind of mixture of ascii and hex. For example Ísland becomes =CDsland. Note, I am using <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> as the first value In the header. Also utf-8 works for all my other forms. Just the multipart form that becomes garbled. Anyways this is my form:

<form action="{{ uploadurl }}" enctype="multipart/form-data" method="post">
  <div><label>Title</label><input type="text" name="title" class="string" /></div>
  <div><label>Picture</label><input type="file" name="img"/></div>
  <div style="margin-top:10px;"><input type="submit" value="Add picture" /></div>
  <input type="hidden" value="{{ album.key }}" name="alid"/>
</form>

这是类处理的形式:

# handler for posting photos
class PostPhoto(blobstore_handlers.BlobstoreUploadHandler):
    def post(self):
        upload_files =  self.get_uploads('img')
        photourl = images.get_serving_url(str(upload_files[0].key()))
        photo = Photo()
        #because of multipart/form-data
        photo.title = self.request.get("title")
        photo.photourl = photourl
        photo.photoalbum = PhotoAlbum.get(self.request.get('alid'))     
        photo.put()

有没有人有一个线索如何解决这个问题?我必须做一些服务器端编码/解码吗?我已经尝试了谷歌搜索没有结果(python newb),所以这是我最后的手段,我只是改变我的设计和分裂的形式。

Does anyone have a clue how I can fix this? Do I have to do some server side encoding/decoding? I have tried googling around for that with no results(python newb), so this is my last resort before i just alter my design and split up the forms.

推荐答案

我使用的是Django nonrel,并用这个中间件修复:

I'm using Django nonrel and fixed it with this middleware:

http://code.google.com/p/googleappengine/issues/detail?id=2749#c33

import logging
import quopri
log = logging.getLogger(__name__)

class BlobRedirectFixMiddleware(object):
    def process_request(self, request):
        if request.method == 'POST' and 'HTTP_X_APPENGINE_BLOBUPLOAD' in request.META and request.META['HTTP_X_APPENGINE_BLOBUPLOAD'] == 'true':
            request.POST = request.POST.copy()
            log.info('POST before decoding: %s' % request.POST)
            for key in request.POST:
                if key.startswith('_') or key == u'csrfmiddlewaretoken':
                    continue
                value = request.POST[key]
                if isinstance(value,(str, unicode)):
                    request.POST[key] = unicode(quopri.decodestring(value), 'iso_8859-2')
            log.info('POST after decoding: %s' % request.POST) 
        return None

这篇关于提交多部分/表单数据表单时,应用引擎中的编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆