Reputation: 18705
I'm trying to serve a zip
file that contains Django
objects images.
The problem is that even if it returns the Zip file, it is corrupted.
NOTE: I can't use absolute paths to files since I use remote storage.
model method that should generate in memory zip
def generate_images_zip(self) -> bytes:
content = BytesIO()
zipObj = ZipFile(content, 'w')
for image_fieldname in self.images_fieldnames():
image = getattr(self, image_fieldname)
if image:
zipObj.writestr(image.name, image.read())
return content.getvalue()
ViewSet action
@action(methods=['get'], detail=True, url_path='download-images')
def download_images(self, request, pk=None) -> HttpResponse:
product = self.get_object()
zipfile = product.generate_images_zip()
response = HttpResponse(zipfile, content_type='application/zip')
response['Content-Disposition'] = 'attachment; filename=images.zip'
return response
When I try to open the downloaded Zip file, it says it's corrupted.
Do you know how to make it work?
Upvotes: 3
Views: 1041
Reputation: 21787
You make the very rookie mistake of not calling close
/ closing the file (ZipFile
here) after opening it, best use the ZipFile
as a context manager:
def generate_images_zip(self) -> bytes:
content = BytesIO()
with ZipFile(content, 'w') as zipObj:
for image_fieldname in self.images_fieldnames():
image = getattr(self, image_fieldname)
if image:
zipObj.writestr(image.name, image.read())
return content.getvalue()
Upvotes: 5