24👍
✅
I don’t know why I had to do this – may be something internal to Django Rest Framework that doesn’t allow putting custom methods onto format?
I simply changed it to the following –
if fileformat == 'raw':
zip_file = open('C:\temp\core\files\CDX_COMPOSITES_20140626.zip', 'rb')
response = HttpResponse(FileWrapper(zip_file), content_type='application/zip')
response['Content-Disposition'] = 'attachment; filename="%s"' % 'CDX_COMPOSITES_20140626.zip'
return response
Then in my URL just hit with the new value and it works fine. I’d love to know why I can’t use format though to serve a file.
13👍
Try using FileWrapper
:
from django.core.servers.basehttp import FileWrapper
...
if format == 'raw':
zip_file = open('C:\temp\core\files\CDX_COMPOSITES_20140626.zip', 'rb')
response = HttpResponse(FileWrapper(zip_file), content_type='application/zip')
response['Content-Disposition'] = 'attachment; filename="%s"' % 'CDX_COMPOSITES_20140626.zip'
return response
...
Also, I would use application/zip
instead of application/force-download
.
👤dgel
- [Django]-How to model a postal address
- [Django]-Can't find msguniq. Make sure you have GNU gettext tools 0.15 or newer installed. (Django 1.8 and OSX ElCapitan)
- [Django]-How to compare two JSON objects with the same elements in a different order equal?
Source:stackexchange.com