[Fixed]-Django download generated file

1👍

May be you should send the file as a chunk , which is memory efficient

import os
import mimetypes
from django.http import StreamingHttpResponse
from wsgiref.util import FileWrapper


def download_file(request):
   the_file = open(file_path, "rb")
   filename = os.path.basename(the_file)
   chunk_size = 8192
   response = StreamingHttpResponse(FileWrapper(open(the_file, 'rb'), chunk_size),
                           content_type=mimetypes.guess_type(the_file)[0])
   response['Content-Length'] = os.path.getsize(the_file)    
   response['Content-Disposition'] = "attachment; filename=%s" % filename
   return response

Leave a comment