Flask How to deal with requests upload Chinese file name problem

Source: Internet
Author: User
Tags rfc

First, the origin of the problem

Recently, a project called Document Services Resource Center, similar to seven cattle, provides a unified file resource service for each business system, including file storage, operation management, download, preview, etc. When doing the file storage, encountered this when the specified upload file name is Chinese, flask framework of the server can not parse into a file, but as a general form of data processing. Our implementation architecture for file storage is as follows:

The client business system (developed by Python) uploads files by calling the above file API in PYTHON-SDK. The file name of the file must be specified in accordance with the format requirements for uploading files in this requests class library. Therefore, after the API development, when the file name of the uploaded files is in Chinese, the test did not pass.

Second, code parsing

Client Test Code:

Note that in the files variable, the first parameter of the tuple value corresponding to file "Thirteen-Five" development plan. docx is the file name, which is in Chinese format.

Service-side Code (Simplified):

Note that in try: Exception in the code to determine whether to get the file successfully.

Run:

Run the service-side code first, and then run the test code. The results are as follows:

Enter debug mode, view the value of the request variable, and focus on the files and Form properties. Such as:

From the can, files of the property is empty, and the file as a property of the form data, the value of the property is the binary content data.

Third, the problem causes of the search

(1.) Download the Fiddler Grab Kit tool. Discover requests delivers the following data to the Flask website service.

Pay special attention to the filename* in the red box.

(2) Read the source code of flask, pay special attention to flask to upload the file parsing and processing. Found in the Parse_lines method in the formparser.py under Werkzeug (413 lines in the file)

It can be concluded that the flask is based on the name filename of the key to determine whether the data passed by requests is the file content. And in the above through the Fiddler Grab Bag tool, requests passed filename* the name of the key, more than a * number. Therefore, flask that the file is not passed, and thus treated as a general attribute.

(3.) Why would the requests pass filename* such a key? Track and read the source code of the requests again. Cashback requests will encode the filename for special processing. The code is located in requestsàpackagesàurllib3->fields.py (line 22nd of the Format_header_param method).

For content that is not ASCII encoded, RFC 2231 is encoded and organized into the key*= RFC 2231 format. So there is a key-value pair of the filename* in this format.

Iv. Solutions

There are three ways to solve this problem.

(1.) Modify the source code of the requests

The following code for the Requestsàpackagesàurllib3->fields.py->format_header_param method

Change into

(2.) Modify the source code of the flask

The following code starts with 413 lines in the Parse_lines method under Werkzeug formparser.py

Change into

and import the appropriate package.

(3.) Modify the filename assignment of the files variable when calling the request's post method, change it to English, such as set to fixed file_name, and the real filename (preferably with a suffix) as the key in the data parameter named File_ The value of name (according to the project situation, the free definition) is passed to the server, and the service side reads the file_name corresponding value on the line. The implementation code is as follows:

Summary: Modifying the source code of the flask, requests class Library is not ideal, it is not easy to deploy, and other unexpected problems may occur. The 3rd compromise solution was recommended.

Flask How to deal with requests upload Chinese file name problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.