首页 > 其他分享 >【django学习-08】视图之HttpResponse文件下载

【django学习-08】视图之HttpResponse文件下载

时间:2022-09-04 22:57:17浏览次数:80  
标签:文件 StreamingHttpResponse 08 视图 django content HttpResponse path FileResponse

  • 一:前言:响应内容除了返回网页信息外,还可以实现文件下载功能,是网站常用的功能之一。Django提供三种方式实现文件下载功能,分别是HttpResponse,StreamingHttpResponse和FileResponse。

  • 二:三种方式说明如下

    • HttpResponse:是所有响应过程的核心类,它的顶层功能是HttpResponseBase;

    • StreamingHttpResponse:在HttpResponseBase的基础上进行继承与重写,它实现流式响应输出,适用于大规模响应和文件传输响应;

    • FileResponse:在StreamingHttpResponse的基础上进行继承与重写,它实现文件流式响应输出,只适合文件传输响应。

    • 查看源码,from django.http import response

      • 2.1:StreamingHttpResponse
class StreamingHttpResponse(HttpResponseBase):
    """
    A streaming HTTP response class with an iterator as content.

    This should only be iterated once, when the response is streamed to the
    client. However, it can be appended to or replaced with a new iterator
    that wraps the original content (or yields entirely new content).
    """

    streaming = True

    def __init__(self, streaming_content=(), *args, **kwargs):
        super().__init__(*args, **kwargs)
        # `streaming_content` should be an iterable of bytestrings.
        # See the `streaming_content` property methods.
        self.streaming_content = streaming_content
  - 参数streaming_content的数据格式可以设置为迭代器对象或者字节流,代表数据或者文件内容;
  -  *args, **kwargs设置HttpResponseBase的参数,即响应内容的数据格式,content_type和响应状态码status等参数。
-:2.2:FileResponse
class FileResponse(StreamingHttpResponse):
    """
    A streaming HTTP response class optimized for files.
    """
    block_size = 4096

    def __init__(self, *args, as_attachment=False, filename='', **kwargs):
        self.as_attachment = as_attachment
        self.filename = filename
        super().__init__(*args, **kwargs)
  - 参数as_attachment的数据类型为布尔值,若为False,则不提供下载功能,文件将会在浏览器中打开;若为True,则开启文件下载功能;
  - filename,设置下载文件的文件名,若as_attachment=False,filename不起作用,若as_attachment=True,参数filename为空,则使用文件原有名称作为下载名称,反之以filename作为下载名称;
  - *args, **kwargs设置HttpResponseBase的参数,即响应内容的数据格式,content_type和响应状态码status等参数。
  • 三:示例
#blog/urls.py
from django.urls import path,re_path,include
from blog import views

urlpatterns = [
    re_path('^download$',views.download),
    re_path('download/file1$',views.download1,name = "download1"),
    re_path('download/file2$',views.download2,name = "download2"),
    re_path('download/file3$',views.download3,name = "download3"),
]

#view.py
from django.http.response import HttpResponse,Http404,StreamingHttpResponse,FileResponse
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent
tu_dir = os.path.join(BASE_DIR,"media")

def download(req):
    return render(req,"dowm.html")

def download1(req):
    # return HttpResponse("download1")
    file_path = os.path.join(tu_dir,'pic1.jpg')
    try:
        r = HttpResponse(open(file_path,'rb'))
        r["content_type"] = 'application/octet-stream'
        r["content-Disposition"] = 'attachment;filename="1.jpg"'
        return r
    except Exception:
        raise Http404("down error")

def download2(req):
    # return HttpResponse("download2")
    file_path = os.path.join(tu_dir, 'pic2.png')
    try:
        r = StreamingHttpResponse(open(file_path, 'rb'))
        r["content_type"] = 'application/octet-stream'
        r["content-Disposition"] = 'attachment;filename="2.jpg"'
        return r
    except Exception:
        raise Http404("down error")

def download3(req):
    # return HttpResponse("download3")
    file_path = os.path.join(tu_dir, 'pic3.jpg')
    try:
        f = open(file_path, "rb")
        r = FileResponse(f, as_attachment=True, filename="3.jpg")
        return r
    except Exception:
        raise Http404("down error")

templates/down.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

    <a href="{% url 'download1' %}">HttpResponse-下载</a>
    <br>
    <a href="{% url 'download2' %}">StreamingHttpResponse-下载</a>
    <br>
    <a href="{% url 'download3' %}">FileResponse-下载</a>
</body>
</html>
  • 四:HttpResponse、StreamingHttpResponse和FileResponse 这三者的差异:

    • HttpResponse 实现文件下载存在很大弊端,其工作原理是将文件读取并载入内存,然后输出到浏览器上实现下载功能。如果文件较大,该方法就会占用很多内存。对于下载大文件,Django推荐使用StreamingHttpResponse 和FileResponse 方法,这两个方法将下载文件分批写入服务器的本地磁盘,减少对内存的消耗。
    • StreamingHttpResponse 和FileResponse 的实现原理是相同的,两者都是将下载文件分批写入本地磁盘,实现文件的流式响应输出。
    • 从适用范围来说,StreamingHttpResponse 的适用范围更为广泛,可支持大规模数据或文件输出,而FileResponse 只支持文件输出。
    • 从使用方式来说,由于StreamingHttpResponse 支持数据或文件输出,因此在使用时需要设置响应输出类型和方式,而FileResponse只需设置3个参数即可实现文件下载功能。

标签:文件,StreamingHttpResponse,08,视图,django,content,HttpResponse,path,FileResponse
From: https://www.cnblogs.com/xwltest/p/16651568.html

相关文章

  • 【2022.9.2】Django框架(网页伪静态、视图层、模板层)
    学习内容概要网页伪静态视图层三板斧JsonResponseform表单上传文件FBV与CBV(核心)CBV源代码(面向对象)模板层模板语法传值模板语法之过滤器模板语法之标签......
  • 2022-2023-1 20221408《计算机基础与程序设计》第一周学习总结
    班级:https://edu.cnblogs.com/campus/besti/2022-2023-1-CFAP作业链接:https://www.cnblogs.com/zhanquanchen/p/16654783.html作业目标:快速浏览教材作业正文:https://www.cn......
  • Django 环境安装
    Django是基于Python的Web框架,依赖Python环境,所以需要提前安装好Python解释器。关于Python的安装,请参考https://www.liujiangblog.com站点中Python教程的相关部分,这里不再......
  • 【2022-08-30】哺乳
    20:00习惯的枷锁,开始的时候轻得难以察觉,到后来却重得无法摆脱。                                ......
  • web框架-django框架
    目录web框架编写web框架模块动静态网页jinjia2模块python主流web框架django框架版本下载安装操作目录结构三板斧静态文件操作form表单request对象方法MySQL数据库连接ORM数......
  • django框架-4
    目录网页伪静态视图层模板层网页伪静态将动态网页伪装成静态网页从而提升网页被搜索引擎收录的概率(掏点票票更快更能提高搜索频率)表现形式就是网址乍一看像一个具......
  • django4/网页伪静态/视图层/模板层
    网页伪静态动态页动态网页,页面代码虽然没有变,但是显示的内容却是可以随着时间、环境或者数据库操作的结果而发生改变的。静态页即静态网页,是实际存在的,无需经过服务器......
  • 【2022-09-02】Django框架(四)
    Django框架(四)Django框架之伪静态概念静态文件:数据是写死,永远不会修改伪静态:将一个动态页面伪装成静态页面#为什么要伪装?伪装的目的在于增大本网站的seo查询力......
  • 简单计数 P6008
    题目传送门题目大意:给定一张边框确定的图,在其中空地放水并满足物理要求,求总方案数题目分析:首先注意到,满足物理需求就是满足在一个连通块内从高度为\(h\)放水,则满足对......
  • A08_System.Threading.Channels使用(针对发布-订阅,消息缓冲处理)
    在面对 生产者-消费者 的场景下,netcore提供了一个新的命名空间 System.Threading.Channels 来帮助我们更高效的处理此类问题,有了这个Channels存在, 生产者 和 ......