118๐
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()
28๐
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.
- [Django]-Django Rest Framework โ Authentication credentials were not provided
- [Django]-Django migration fails with "__fake__.DoesNotExist: Permission matching query does not exist."
- [Django]-What's the difference between select_related and prefetch_related in Django ORM?
11๐
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
- [Django]-Django content-type : how do I get an object?
- [Django]-Django: FloatField or DecimalField for Currency?
- [Django]-Django models.py Circular Foreign Key
9๐
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.
- [Django]-What's the best way to extend the User model in Django?
- [Django]-What are the differences between django-tastypie and djangorestframework?
- [Django]-Django apps aren't loaded yet when using asgi
7๐
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)
- [Django]-Get the list of checkbox post in django views
- [Django]-Django: Display Choice Value
- [Django]-What's the best way to extend the User model in Django?
5๐
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.
- [Django]-Where is a good place to work on accounts/profile in Django with the Django registration app?
- [Django]-How to update an existing Conda environment with a .yml file
- [Django]-How do I raise a Response Forbidden in django
4๐
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
- [Django]-Django using get_user_model vs settings.AUTH_USER_MODEL
- [Django]-How to obtain and/or save the queryset criteria to the DB?
- [Django]-Changing a project name in django