Closed bmampaey closed 4 years ago
I believe the problem comes from this piece of code in VirtualFile
def _get_size(self):
try:
return self._size
except AttributeError:
try:
self._size = self.file.size
except AttributeError:
self._size = len(self.file.getvalue()) <======= LOOK HERE ===================
return self._size
TextIteratorIO does not have the getvalue method, so it raises an AttributeError that is caught in DownloadResponse's method default_headers:
try:
headers['Content-Length'] = self.file.size
except (AttributeError, NotImplementedError):
pass # Generated files.
But BytesIteratorIO inherits from io.BytesIO that does implement getvalue, and so it returns 0.
Similarly to the example given in the documentation for VirtualDownloadView for Stream generated content, i tried to make a view that returns a zipfile from a generator. Since zipfiles are binary, I use the BytesIteratorIO instead of the TextIteratorIO to wrap my generator.
The problem is that in that case, the response header Content-Length is set to zero, which causes the browser to not download the file.
Here is an example to demonstrate the problem, with both TextIteratorIO and BytesIteratorIO
The urls set up (for clarity):
And here is the result from wget on both views:
PS: I used django version 1.9.7and django_downloadview 1.9