import gzip
from django.http import FileResponse
def some_request(request):
return FileResponse(
gzip.open('/path/to/filename.gz'),
content_type="text/plain",
filename="filename.txt",
as_attachment=True
)
if os.path.isabs(filename):
self['Content-Length'] = os.path.getsize(filelike.name)
elif hasattr(filelike, 'getbuffer'):
self['Content-Length'] = filelike.getbuffer().nbytes
from io import BytesIO
import shutil
def some_request(request):
buff = BytesIO()
with gzip.open('/path/to/filename.gz') as g:
shutil.copyfileobj(g, buff)
buff.seek(0)
return FileResponse(
buff,
content_type="text/plain",
filename="filename.txt",
as_attachment=True
)