写在最前面:
不要用ajax请求下载文件,只会返回类似json的数据。因为ajax不支持。
可以用form形式请求。
例如
$('.export_data').click(function () {
Toastr('message', "开始下载数据", 'success') const currentUrl = window.location.href var $editor = ace.edit("editor"); var input_sql = $editor.getValue() var url = currentUrl + 'export_data/'; console.log(url) /** * 使用form表单来发送请求 * 1.method属性用来设置请求的类型――post还是get * 2.action属性用来设置请求路径。 * */ var form=$("<form>");//定义一个form表单 form.attr("style","display:none"); form.attr("target",""); form.attr("method","get"); //请求类型 form.attr("action",url); //请求地址 var sql=$("<input>"); sql.attr("type","hidden"); sql.attr("name","sql"); sql.attr("value",input_sql); form.append(sql); $(document.body).append(form) form.submit() });
最简单的文件下载功能的实现
将文件流放入HttpResponse对象即可,如:
1
2
3
4
5
|
def
file_download
(
request
)
:
# do something...
with
open
(
'file_name.txt'
)
as
f
:
c
=
f
.
read
(
)
return
HttpResponse
(
c
)
|
这种方式简单粗暴,适合小文件的下载,但如果这个文件非常大,这种方式会占用大量的内存,甚至导致服务器崩溃
更合理的文件下载功能
Django的HttpResponse
对象允许将迭代器作为传入参数,将上面代码中的传入参数c
换成一个迭代器,便可以将上述下载功能优化为对大小文件均适合;而Django更进一步,推荐使用 StreamingHttpResponse
对象取代HttpResponse
对象,StreamingHttpResponse
对象用于将文件流发送给浏览器,与HttpResponse对象非常相似,对于文件下载功能,使用StreamingHttpResponse
对象更合理。
因此,更加合理的文件下载功能,应该先写一个迭代器,用于处理文件,然后将这个迭代器作为参数传递给StreaminghttpResponse
对象,如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
from
django
.
http
import
StreamingHttpResponse
def
big_file_download
(
request
)
:
# do something...
def
file_iterator
(
file_name
,
chunk_size
=
512
)
:
with
open
(
file_name
)
as
f
:
while
True
:
c
=
f
.
read
(
chunk_size
)
if
c
:
yield
c
else
:
break
the_file_name
=
"file_name.txt"
response
=
StreamingHttpResponse
(
file_iterator
(
the_file_name
)
)
return
response
|
文件下载功能再次优化
上述的代码,已经完成了将服务器上的文件,通过文件流传输到浏览器,但文件流通常会以乱码形式显示到浏览器中,而非下载到硬盘上,因此,还要在做点优化,让文件流写入硬盘。优化很简单,给StreamingHttpResponse对象的Content-Type
和Content-Disposition
字段赋下面的值即可,如:
1
2
|
response
[
'Content-Type'
]
=
'application/octet-stream'
response
[
'Content-Disposition'
]
=
'attachment;filename="test.pdf"'
|
完整代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
from
django
.
http
import
StreamingHttpResponse
def
big_file_download
(
request
)
:
# do something...
def
file_iterator
(
file_name
,
chunk_size
=
512
)
:
with
open
(
file_name
)
as
f
:
while
True
:
c
=
f
.
read
(
chunk_size
)
if
c
:
yield
c
else
:
break
the_file_name
=
"big_file.pdf"
response
=
StreamingHttpResponse
(
file_iterator
(
the_file_name
)
)
response
[
'Content-Type'
]
=
'application/octet-stream'
response
[
'Content-Disposition'
]
=
'attachment;filename="{0}"'
.
format
(
the_file_name
)
return
response
|