zoukankan      html  css  js  c++  java
  • django 中实现文件下载的3种方式


    方法一:使用HttpResponse
    from django.shortcuts import HttpResponse def file_down(request): file=open('/home/amarsoft/download/example.tar.gz','rb') response =HttpResponse(file) response['Content-Type']='application/octet-stream' response['Content-Disposition']='attachment;filename="example.tar.gz"' return response 方法二:使用StreamingHttpResponse from django.http import StreamingHttpResponse def file_down(request): file=open('/home/amarsoft/download/example.tar.gz','rb') response =StreamingHttpResponse(file) response['Content-Type']='application/octet-stream' response['Content-Disposition']='attachment;filename="example.tar.gz"' return response 方法三:使用FileResponse from django.http import FileResponse def file_down(request): file=open('/home/amarsoft/download/example.tar.gz','rb') response =FileResponse(file) response['Content-Type']='application/octet-stream' response['Content-Disposition']='attachment;filename="example.tar.gz"' return response

    总结:对比 虽然使用这三种方式都能实现,但是推荐用FileResponse, 在FileResponse中使用了缓存,更加节省资源。 虽说是三种方式,但是原理相同,说白了就是一种方式。
  • 相关阅读:
    抽象与接口的综合练习
    java构造函数能否被继承,为什么?
    题解 【USACO 4.2.1】草地排水
    题解 【NOI2010】超级钢琴
    题解 [USACO Mar08] 奶牛跑步
    题解 【NOIP2016】魔法阵
    题解 对称二叉树
    题解 【NOIP2014】解方程
    题解 【NOIP2010】关押罪犯
    题解 贪吃蛇
  • 原文地址:https://www.cnblogs.com/lvye001/p/11586413.html
Copyright © 2011-2022 走看看