Is it possible to make a zip archive and offer it to download, but still not save a file to the hard drive?
7 Answers
To trigger a download you need to set Content-Disposition
header:
from django.http import HttpResponse
from wsgiref.util import FileWrapper
# generate the file
response = HttpResponse(FileWrapper(myfile.getvalue()), content_type='application/zip')
response['Content-Disposition'] = 'attachment; filename=myfile.zip'
return response
If you don't want the file on disk you need to use StringIO
import cStringIO as StringIO
myfile = StringIO.StringIO()
while not_finished:
# generate chunk
myfile.write(chunk)
Optionally you can set Content-Length
header as well:
response['Content-Length'] = myfile.tell()
-
1I think Content-Length might happen automatically with Django middleware – andrewrk Jun 18 '10 at 00:28
-
4Using this example downloads a file that is always empty, any ideas? – camelCase May 22 '13 at 23:30
-
3As @eleaz28 said, it was creating blank files in my case too. I just removed the `FileWrapper`, and it worked. – Seb D. May 05 '15 at 15:57
-
This answer doesn't work with Django 1.9: see this: http://stackoverflow.com/a/35485073/375966 – Afshin Mehrabani Feb 18 '16 at 15:06
-
I know this post is old now, but @eleaz28, i was having the same trouble, i solved it by using fileIO.seek(0). – Dennis Jan 24 '17 at 17:25
-
@AfshinMehrabani, to only download file in django 1.10 there's a documentation now: https://docs.djangoproject.com/en/1.10/ref/request-response/#telling-the-browser-to-treat-the-response-as-a-file-attachment – Dennis Jan 24 '17 at 17:25
-
2I opened my file in read mode then file.getvalue() is giving attribute error : TextIOWrapper has no attribute getValue . – Anonymous Apr 13 '18 at 20:41
-
New versions of Django provide FileResponse which is a wrapper around wsgiref FileWrapper but generates streaming http response: https://docs.djangoproject.com/en/3.0/ref/request-response/#fileresponse-objects – Pratyush Apr 24 '20 at 05:57
You'll be happier creating a temporary file. This saves a lot of memory. When you have more than one or two users concurrently, you'll find the memory saving is very, very important.
You can, however, write to a StringIO object.
>>> import zipfile
>>> import StringIO
>>> buffer= StringIO.StringIO()
>>> z= zipfile.ZipFile( buffer, "w" )
>>> z.write( "idletest" )
>>> z.close()
>>> len(buffer.getvalue())
778
The "buffer" object is file-like with a 778 byte ZIP archive.

- 384,516
- 81
- 508
- 779
-
2Good point about saving memory. But if using a temporary file, where would you put the code to delete it? – andrewrk Jun 18 '10 at 00:28
-
@superjoe30: periodical cleanup jobs. Django already has an admin command that must be run periodically to remove old sessions. – S.Lott Jun 18 '10 at 02:51
-
-
@S.Lott Is it possible to serve the created file (z in in your example) using mod x-sendfile? – Amir Afianian Jan 24 '16 at 11:48
Why not make a tar file instead? Like so:
def downloadLogs(req, dir):
response = HttpResponse(content_type='application/x-gzip')
response['Content-Disposition'] = 'attachment; filename=download.tar.gz'
tarred = tarfile.open(fileobj=response, mode='w:gz')
tarred.add(dir)
tarred.close()
return response

- 2,586
- 16
- 25

- 111
- 1
- 2
-
1For newer version of Django, you should have `content_type=` instead of `mimetype=` – Guillaume Lebreton Jan 26 '18 at 08:14
Yes, you can use the zipfile module, zlib module or other compression modules to create a zip archive in memory. You can make your view write the zip archive to the HttpResponse
object that the Django view returns instead of sending a context to a template. Lastly, you'll need to set the mimetype to the appropriate format to tell the browser to treat the response as a file.

- 88,194
- 49
- 192
- 260
models.py
from django.db import models
class PageHeader(models.Model):
image = models.ImageField(upload_to='uploads')
views.py
from django.http import HttpResponse
from StringIO import StringIO
from models import *
import os, mimetypes, urllib
def random_header_image(request):
header = PageHeader.objects.order_by('?')[0]
image = StringIO(file(header.image.path, "rb").read())
mimetype = mimetypes.guess_type(os.path.basename(header.image.name))[0]
return HttpResponse(image.read(), mimetype=mimetype)

- 1
- 1

- 71
- 1
- 1
def download_zip(request,file_name):
filePath = '<path>/'+file_name
fsock = open(file_name_with_path,"rb")
response = HttpResponse(fsock, content_type='application/zip')
response['Content-Disposition'] = 'attachment; filename=myfile.zip'
return response
You can replace zip and content type as per your requirement.

- 6,203
- 6
- 39
- 50
Same with in memory tgz archive:
import tarfile
from io import BytesIO
def serve_file(request):
out = BytesIO()
tar = tarfile.open(mode = "w:gz", fileobj = out)
data = 'lala'.encode('utf-8')
file = BytesIO(data)
info = tarfile.TarInfo(name="1.txt")
info.size = len(data)
tar.addfile(tarinfo=info, fileobj=file)
tar.close()
response = HttpResponse(out.getvalue(), content_type='application/tgz')
response['Content-Disposition'] = 'attachment; filename=myfile.tgz'
return response

- 3,363
- 3
- 32
- 62