目录
- 使用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实现下载文件