python3 download files using django, Chinese name garbled how do?

Some time ago was a front-end small cute despise a bit, not a year that I have updated the blog, I refused to accept, obviously there are two months to a year before it. But that being said, or to update the look of.
These are an excuse to start the following text.
 
 
I have an internal company system, with django done, the project will inevitably place to download the file, before lazy, I always use django own way, in total urls.py project
 
urlpatterns += static(FILEPATH, document_root=FILEPATH)

 

Such solutions.
 
However, this method has a great flaw: written test environment can play, make sure a formal environmental settings in debug = True switched off. And this method, after turn off debug, can not be used.
 
So I had to go the traditional route: Set header.
 
Increase in urls.py in:
 
url(r"^download/$", views.download, name="download")

 

In the page:
 
<a href="{% url 'download' %}">下载</a>

 

In view of a function to increase the views.py:
 
DEF download (Request):
     return build_download_response (FILE_PATH, " I love Coke .docx " )

 

(MTV Yaojin three tricks mode, nothing wrong)
 
Because the system uses more than one place inside the download function, so I put it into a generic function:
 
from django.http import FileResponse
 
def build_download_response(filepath, filename):
    """
    构建下载文件的文件头
    :param filepath: 文件路径
    :param filename: 文件名
    :return: FileResponse
    """
    absname = os.path.join(filepath, filename) if os.path.isdir(filepath) else filepath
 
    response = FileResponse(open(absname, "rb"))
    response["Content-Type"] = "application/octet-stream"
    response["Content-Disposition"] = "attachment; filename='%s'" % filename
    return response
 

 

Referring to the online statement, this is no problem. Blue Goose, it really is a problem: When I click on the download button, pop-up interface only "download" word.
 
 
Meow meow meow? I file it?
I try to download a file looked down from a size point of view, there is no problem.
 
 
I put the name change overnight, you can normally open, which does not throw things - in other words, just the file name out of the question .
But the file name can have a hair problem ah? You can not bully me who only Chinese company now!
 
 
 
After a fierce and exciting to Baidu search, I learned from this blog  https://blog.csdn.net/u011090495/article/details/18815777  found out why in.
Simple point that is, Content-Disposition filename inside this thing, if they are not RFC standard, supports only ASCII-encoded file name. If the file name is not in English, I am afraid it will name garbled, or the situation appears to be renamed . So how to do it? Another RFC to add an extension, you can define a filename *, and file name encoding it.
So, I put the Content-Disposition from here
 
response["Content-Disposition"] = "attachment; filename='%s'" % filename

 

Changed
 
response["Content-Disposition"] = "attachment; filename='%s'; filename*=UTF-8''%s" % (filename.encode("UTF-8"), filename.encode("UTF-8"))

 

The results of test it, crying.
 
 
Really it is coded ......
I want trouble.
 
Fortunately, later in  https://segmentfault.com/q/1010000009078463  I found the answer to this post. Original filename * = UTF-8 '' % s "% filename.encode (" UTF-8 ") is used regardless write, django django own method of:
 
from django.http import FileResponse
from django.utils.encoding import escape_uri_path
 
def build_download_response(filepath, filename):
    """
    构建下载文件的文件头
    :param filepath: 文件路径
    :param filename: 文件名
    :return: FileResponse
    """
    absname = os.path.join(filepath, filename) if os.path.isdir(filepath) else filepath
 
    response = FileResponse(open(absname, "rb"))
    response["Content-Type"] = "application/octet-stream"
    response["Content-Disposition"] = "attachment; filename*=UTF-8''{}".format(escape_uri_path(filename))
 
    return response
 

 

So, you can normally download.
 
 
 
Open look, content no problem!
 
 
 
Happy! (I will not take the matter to the front to show off too!)
 

Guess you like

Origin www.cnblogs.com/anpengapple/p/11431266.html