From Dajngo docs FileResponse is a subclass of StreamingHttpResponse
optimized for binary files.
import os
from django.http import FileResponse
from django.views.decorators.http import require_POST
@require_POST
def download(request):
article = Article.objects.get(id=1)
fullpath = article.file.path
if not os.path.exists(fullpath):
raise Http404('{0} does not exist'.format(fullpath))
return FileResponse(
open(fullpath, 'rb'), as_attachment=True,
filename=article.file.name)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…