Issue #1277 new

wrong encode issue form-data text

Yi Liu
created an issue

I use cherrypy 3.2.4 with python2.7, run the example file upload code in the official doc.

#coding: utf-8
__author__ = 'liuyix'

import os
localDir = os.path.dirname(__file__)
absDir = os.path.join(os.getcwd(), localDir)

import cherrypy

class FileDemo(object):

    def index(self):
        return """
            <h2>Upload a file</h2>
            <form action="upload" method="post" enctype="multipart/form-data" accept-charset="UTF-8">
            filename: <input type="file" name="my_file" /><br />
            <input type="submit" />
            <h2>Download a file</h2>
            <a href='download'>This one</a>
        """ = True

    def upload(self, my_file):
        print cherrypy.request.config
        out = """<html>
            my_file length: %s<br />
            my_file filename: %s<br />
            my_file mime-type: %s<br />
            attempt charset: %s<br />
            filename: %s<br />

        # Although this just counts the file length, it demonstrates
        # how to read large files in chunks instead of all at once.
        # CherryPy reads the uploaded file into a temporary file;
        # reads from that.
        size = 0
        while True:
            data =
            if not data:
            size += len(data)
        #print 'utf-8 encoding:'
        #print 'filename: %s' % type(my_file)
        print 'filename: %s' % repr(my_file.filename)
        #print 'attempt: %s' % str(my_file.attempt_charsets)
        #print 'charset: %s' % str(my_file.charset)
        # this line is working!!!!
        filename = my_file.filename.encode('iso-8859-1').decode('utf8')
        #filename = my_file.filename
        #print 'chinese name: %s' % filename.decode('utf8')
        return out % (size, filename, my_file.content_type, str(my_file.attempt_charsets), my_file.filename) = True

tutconf = os.path.join(os.path.dirname(__file__), 'tutorial.conf')

if __name__ == '__main__':
    # CherryPy always starts with app.root when trying to map request URIs
    # to objects, so we need to mount a request handler root. A request
    # to '/' will be mapped to HelloWorld().index().
    cherrypy.quickstart(FileDemo(), config=tutconf)
    # This branch is for the test suite; you can ignore it.
    cherrypy.tree.mount(FileDemo(), config=tutconf)

When the upload file is Chinese character, the output filename is messed. I have tried tools.encode and tools.decode but not both working.

When I try this line " my_file.filename.encode('iso-8859-1').decode('utf8')" , the filename is correct.

Comments (0)

  1. Log in to comment