天地一扁舟 2015-10-28 07:02 采纳率: 0%
浏览 2096
已结题

请教各位大神,为什么django下载的zip压缩包文件名中包含中文的就乱码啊?

各位朋友们好,我用django写了一个网页,可以下载zip的压缩包文件,但是如果该zip文件名含有中文,比如如果zip文件名是”math.zip”,那么下载的时候显示的文件名也是”math.zip”,但是如果我提供的下载文件名是”1.1数的分类”,就会出现如” 79d6c6d5”这种乱码文件
下面是我的django处理下载zip压缩包的代码:

 def zip_download(request):
   try:
        temp = open(zipfileName  , "rb")
    except IOError as ex:
        return HttpResponse("Open zip error")
    wrapper = FileWrapper(temp)
    response = HttpResponse(wrapper, content_type='application/zip')
    realZipfileName = os.path.split(zipfileName)[1]
    response['Content-Encoding'] = 'utf-8'
    response['Content-Disposition'] = 'attachment; filename=%s' % realZipfileName
    response['Content-Length'] = temp.tell()
    temp.seek(0)
return response

请诸位大神指点一下,如何才能避免下载带有中文的zip压缩包乱码的问题,在此先谢过各位朋友。

  • 写回答

1条回答 默认 最新

  • 逝_去_的_光_阴 2015-10-28 08:35
    关注

    给文件名转码吧 试试统一转成utf-8

    评论

报告相同问题?