Py学习  »  问与答

请问djang怎么下载文件啊

A-Meng • 9 年前 • 6762 次点击  

试了网上的好多方法,都不行。

快急疯了,请各位帮忙啊

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/979
 
6762 次点击  
文章 [ 12 ]  |  最新文章 8 年前
A-Meng
Reply   •   1 楼
A-Meng    8 年前

@saibei88 通过urlquote转换文件名后,在ie下中文正常,在Firefox下中文就是乱码,请问怎么解决啊

calm
Reply   •   2 楼
calm    9 年前

编辑器编辑出来的有点乱。。。如果看不清楚或有问题就qq我,1041315735@qq.com

calm
Reply   •   3 楼
calm    9 年前

from rexec import FileWrapper#安装这个包 wrapper = FileWrapper(open(pull_path, "rb"))#pull_path是文件绝对路径 response = HttpResponse(wrapper, mimetype='application/%s' % strSuffix)#strSuffix下载文件名 response['Content-Type'] = "application/%s;charset=utf-8" % strSuffix try: strFileName=strFileName.encode('gb2312') #IE下乱码,转换中文编码 content_disposition = 'attachment;filename="%s"' % strFileName response['Content-Disposition'] = content_disposition except Exception, e: response['Content-Disposition'] = 'attachment; filename=%s' % strFileName return response

A-Meng
Reply   •   4 楼
A-Meng    9 年前

@saibei88 谢谢,现在我在ie11下中文名的文件下载下来是正确的,在firefox下就是乱码。

A-Meng
Reply   •   5 楼
A-Meng    9 年前

@amaozhao 多谢了,能否再详细一些

saibei88
Reply   •   6 楼
saibei88    9 年前

@A-Meng urlquote 就是把字符变成 url编码格式 %3D 就是 = 这种

amaozhao
Reply   •   7 楼
amaozhao    9 年前

下载文件一般都是用apache或nginx代理来处理, django只负责跳转就好.(个人建议)

A-Meng
Reply   •   8 楼
A-Meng    9 年前

还是不行,在ie下通过了,firefox下还是乱码,晕了啊,请高手解决

A-Meng
Reply   •   9 楼
A-Meng    9 年前

在这里解决的:http://blog.csdn.net/wlx3351/article/details/6549328 将2改为“response['Content-Disposition'] = 'attachment; filename="' + urlquote(file_name) +'"' ”

urlquote是干什么用的?

A-Meng
Reply   •   10 楼
A-Meng    9 年前

response['Content-Disposition'] = 'attachment; filename = %s' % file_name在格式化中文名时出错了,不知道怎么解决啊

A-Meng
Reply   •   11 楼
A-Meng    9 年前

用这里的方法解决了: http://oldj.net/article/django-big-file-response/

不过得添加

  1. response['Content-Type'] = 'application/octet-stream'
  2. response['Content-Disposition'] = 'attachment; filename = %s' % file_name 两句描述

而且只能下载英文文件,中问文件无法读取正确的文件名,不知道为什么

A-Meng
Reply   •   12 楼
A-Meng    9 年前

我用的django1.7+python3.4