为什么我在 get_uploads 中出现 blobstore_handlers.py 第 348 行错误?

发布于 2024-12-10 13:06:12 字数 2997 浏览 0 评论 0原文

我修补了 appengine_config 以使用 blobstoreuploadhandler 启用外来字符:

webapp_django_version = '1.2'
import base64
import quopri

from webob import multidict


def from_fieldstorage(cls, fs):

    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)

现在,当使用只有一个文件的表单执行上传时,我收到此错误。是因为我还需要文本值吗?如果我删除 appengine_config 的补丁,那么我的上传可以工作,但外部字符会中断。我是否应该尝试仅添加一个隐藏变量,以便提交的所有内容都不仅仅是一个文件?谢谢你的任何好主意

Traceback (most recent call last):
  File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/_webapp25.py", line 703, in __call__
    handler.post(*groups)
  File "/base/data/home/apps/s~montaoproject/deleteoredit.354032237980992086/main.py", line 2659, in post
    for upload in self.get_uploads():
  File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/blobstore_handlers.py", line 348, in get_uploads
    for key, value in self.request.params.items():
  File "/base/python_runtime/python_lib/versions/1/webob/__init__.py", line 951, in params
    params = self.str_params
  File "/base/python_runtime/python_lib/versions/1/webob/__init__.py", line 943, in str_params
    return NestedMultiDict(self.str_GET, self.str_POST)
  File "/base/python_runtime/python_lib/versions/1/webob/__init__.py", line 870, in str_POST
    vars = MultiDict.from_fieldstorage(fs)
  File "/base/data/home/apps/s~montaoproject/deleteoredit.354032237980992086/appengine_config.py", line 44, in from_fieldstorage
    obj.add(field.name, field_value)
UnboundLocalError: local variable 'field_value' referenced before assignment

I patched appengine_config to enable foreign characters with blobstoreuploadhandler:

webapp_django_version = '1.2'
import base64
import quopri

from webob import multidict


def from_fieldstorage(cls, fs):

    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)

Now I get this error when performing an upload with a form that has just one file. Is it because I also need text values? If I remove the patch to appengine_config then my upload works but the foreign charcater breaks. Should I try adding just a hidden variable so that everyting that's submitted isn't just a file? Thanks for any good idea

Traceback (most recent call last):
  File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/_webapp25.py", line 703, in __call__
    handler.post(*groups)
  File "/base/data/home/apps/s~montaoproject/deleteoredit.354032237980992086/main.py", line 2659, in post
    for upload in self.get_uploads():
  File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/blobstore_handlers.py", line 348, in get_uploads
    for key, value in self.request.params.items():
  File "/base/python_runtime/python_lib/versions/1/webob/__init__.py", line 951, in params
    params = self.str_params
  File "/base/python_runtime/python_lib/versions/1/webob/__init__.py", line 943, in str_params
    return NestedMultiDict(self.str_GET, self.str_POST)
  File "/base/python_runtime/python_lib/versions/1/webob/__init__.py", line 870, in str_POST
    vars = MultiDict.from_fieldstorage(fs)
  File "/base/data/home/apps/s~montaoproject/deleteoredit.354032237980992086/appengine_config.py", line 44, in from_fieldstorage
    obj.add(field.name, field_value)
UnboundLocalError: local variable 'field_value' referenced before assignment

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

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

发布评论

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

评论(1

拒绝两难 2024-12-17 13:06:12

缩进“obj.add(field.name, field_value)”,使其成为 else 语句的一部分。

Indent "obj.add(field.name, field_value)" so that it is part of your else statement.

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