django 中实现文件下载的3种方式
生活随笔
收集整理的這篇文章主要介紹了
django 中实现文件下载的3种方式
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
方法一:使用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
''' 遇到問題沒人解答?小編創(chuàng)建了一個(gè)Python學(xué)習(xí)交流QQ群:531509025 尋找有志同道合的小伙伴,互幫互助,群里還有不錯(cuò)的視頻學(xué)習(xí)教程和PDF電子書! ''' 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總結(jié)
以上是生活随笔為你收集整理的django 中实现文件下载的3种方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python如何保证输入键入数字
- 下一篇: 常用正则表达大全