I'm trying to put a zip file into an io.BytesIO buffer and then offer that for download. Below is what I've got (part of a longer views.py, I'm just posting the relevant part).
But I'm getting the following error message:
AttributeError at 'bytes' object has no attribute 'read'
Can anyone tell me what I'm doing wrong?
from django.http import HttpResponse
from wsgiref.util import FileWrapper
from zipfile import *
import io
buffer = io.BytesIO()
zipf = ZipFile(buffer, "w")
zipf.write ("file.txt")
zipf.close()
response = HttpResponse(FileWrapper(buffer.getvalue()), content_type='application/zip')
response['Content-Disposition'] = 'attachment; filename=file.zip'
return response
Edit: It's telling me the error is coming from the line:
response = HttpResponse(FileWrapper(buffer.getvalue()), content_type='application/zip')