python Django 文件下载示例

 1 from django.http import StreamingHttpResponse#文件流
 2 
 3 
 4 def big_file_download(request):
 5     # do something...
 6 
 7     def file_iterator(file_name, chunk_size=512):
 8         with open(file_name,'rb') as f:
 9             while True:
10                 c = f.read(chunk_size)
11                 if c:
12                     yield c
13                 else:
14                     break
15 
16     the_file_name = "big_file.pdf"
17     response = StreamingHttpResponse(file_iterator(the_file_name))
18     response['Content-Type'] = 'application/octet-stream'
19     response['Content-Disposition'] = 'attachment;filename="{0}"'.format(the_file_name)
20 
21     return response
22 
23 链接:http://www.jianshu.com/p/2ce715671340
25 來源:简书  作者:Gevin

 

 
posted @ 2017-10-13 09:37  莫柔落切  阅读(948)  评论(0编辑  收藏  举报