码迷,mamicode.com
首页 > 其他好文 > 详细

【Django】Django 文件下载最佳实践

时间:2016-10-18 13:24:08      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

代码:

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

 

 

参考资料:

http://www.jianshu.com/p/2ce715671340

http://blog.csdn.net/martin_liang/article/details/43286539

http://zhidao.baidu.com/link?url=l2plQ2oAU0A-SJzEH-OwWsLVciU91XlQwMmn3qrXhHkY9XRDFeSv09YAfQpVKZbrmKzOSFLgtA3mGmtTTjgGzJHzMI7u9WpdozQFwxq0fNW

http://www.python88.com/topic/126/

【Django】Django 文件下载最佳实践

标签:

原文地址:http://www.cnblogs.com/junneyang/p/5972761.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!