通过 multipart/form-data 发布时外来字符中断

发布于 2024-12-28 13:37:47 字数 530 浏览 1 评论 0原文

我在 google-app-engine 上使用 django-nonrel 。

当我发布一个外国字符(

在我的例子中是韩国字符)时,它带有多部分/表单数据,它正在被破坏。

<form method="post" enctype="multipart/form-data" action=".">

例如,如果我发布字符串“한글”,

它将在我的数据库中记录为字符串“7ZWc6riA”。

根据我的研究,这是 jsp 中的常见情况,

在 Java 中解决如下:

String name = multipartRequest.getParameter("name");
name = new String(name.getBytes("8859_1"),"utf-8");

但是,我无法在 Django 中找到等效项,

也不太确定是否可以用相同的逻辑解决我的问题。

任何帮助/线索将不胜感激。

I am using django-nonrel on the google-app-engine.

When I'm posting a foreign character,

in my case Korean Character, with a multipart/form-data it is breaking.

<form method="post" enctype="multipart/form-data" action=".">

For example, if I post a string '한글'

it is recorded in my database as a string '7ZWc6riA'.

From my research this is the common case in jsp,

and in Java it's solve as below:

String name = multipartRequest.getParameter("name");
name = new String(name.getBytes("8859_1"),"utf-8");

However, I was unable to find the equivalent in Django,

nor not quite sure if I can solve my problem with the same logic.

Any help/clue will be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫腻 2025-01-04 13:37:47

我发现了这个问题的一个未解决的问题。

问题 2749:Blobstore 处理程序破坏数据编码
http://code.google.com/p/googleappengine/issues/detail ?id=2749

您可以在上面的链接中找到几种不同的选项来解决此错误。

就个人而言,作为 Django 非相关用户,我会采用如下所示的解决方案:

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

I found an open issue for this problem.

Issue 2749: Blobstore handler breaking data encoding
http://code.google.com/p/googleappengine/issues/detail?id=2749

You can find several different options to go around this bug in the link above.

Personally, as a Django-nonrel user, I'd go with a solution shown below:

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
狼性发作 2025-01-04 13:37:47

问题很可能出在您所提供的 HTML 而不是 Django。我正在使用 HTML5,我只需要在我的元素中添加这个元标记。我尝试过各种语言,它们都输入得很好。

<head>
<meta charset="UTF-8" />
</head>

The problem is most likely with the HTML that you are serving rather than Django. I'm using HTML5, I just need this meta tag in my element. I've tried various languages, and they all input fine.

<head>
<meta charset="UTF-8" />
</head>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文