Django实现文件下载

目录

  • 使用StreamingHttpResponse
  • 使用FileResponse
  • 返回Django目录

使用StreamingHttpResponse

views中主要代码:

from django.http import StreamingHttpResponse
def download(request):
  file=open('crm/models.py','rb')
  response =StreamingHttpResponse(file)
  response['Content-Type']='application/octet-stream'
  response['Content-Disposition']='attachment;filename="models.py"'
  return response

 

使用FileResponse

views中主要代码:

from django.http import FileResponse
def download(request):
  file=open('crm/models.py','rb')
  response =FileResponse(file)
  response['Content-Type']='application/octet-stream'
  response['Content-Disposition']='attachment;filename="models.py"'
  return response

 

see also:详解django三种文件下载方式 | Django实现下载文件

posted @ 2019-12-30 17:13  若如初见_you  阅读(316)  评论(0编辑  收藏  举报