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

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

问题描述

我有一个简单的表单,用于向 blobstore 提交图像和图像的标题.这适用于我的本地开发服务器,但是当我部署我的代码时,标题中的非 ascii 字母会因某种 ascii 和十六进制的混合而变得乱码.例如 Ísland 变成 =CDsland.请注意,我使用 <meta http-equiv="Content-Type" content="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天全站免登陆