下载的东西是这样,不管是什么文件名字都是这,这是什么鬼??
![]()
改善后下载的中文档案 会显示名字:

改善后代码:
from django.utils.http import urlquote
file_name_chinese = obj.files.first().name
response['Content-Disposition'] = 'attachment;filename="%s"'%(urlquote(file_name_chinese))
outfile = open(filename, "rb")
response = FileResponse(outfile)
response['Content-Type'] = 'application/octet-stream'
from django.utils.http import urlquote
file_name_chinese = obj.files.first().name
response['Content-Disposition'] = 'attachment;filename="%s"'%(urlquote(file_name_chinese))
print(obj.files.first().name)
return response
原代码:
outfile = open(filename, "rb")
response = FileResponse(outfile)
response['Content-Type'] = 'application/octet-stream'
response['Content-Disposition'] = 'attachment;filename="%s"' % obj.files.first().name
return response

本文介绍了一种解决在Django应用中下载中文文件名时出现乱码问题的方法。通过使用urlquote函数对文件名进行编码,确保了下载的中文文件名能够正确显示。
6395

被折叠的 条评论
为什么被折叠?



