python - Zipping files in Django view and serving them -
first of want know bad serve files django situation can handled django chose serve zipped files.in models.py have model
class documents(models.model): filename = models.charfield(max_length=100) document = models.filefield(upload_to='docs') allowedgroup = models.manytomanyfield(group)
so when normal user log-in displayed documents has permission according his/her group.i want user able download multiple documents(files) @ 1 go.so did added handler downloading multiple files zip file:
i used django snippet creating view
def download_selected_document(self, request, queryset): if len(queryset)>1: temp = tempfile.temporaryfile() archive = zipfile.zipfile(temp, 'w', zipfile.zip_deflated) in queryset: ##reading file content content = open(settings.media_root+str(i.document),'rb').read() ##name name of file "abc.docx" name = str(queryset[0].document)[10:] ##at gives me error archive.write(content,name) archive.close() wrapper = filewrapper(temp) response = httpresponse(wrapper, content_type='application/zip') response['content-disposition'] = 'attachment; filename=test.zip' response['content-length'] = temp.tell() temp.seek(0) return response else: self.message_user(request, "you must select multiple documents downloading.")
error got : must encoded string without null bytes, not str
traceback: file "c:\python27\lib\site-packages\django\core\handlers\base.py" in get_response 111. response = callback(request, *callback_args, **callback_kwargs) file "c:\python27\lib\site-packages\django\contrib\admin\options.py" in wrapper 307. return self.admin_site.admin_view(view)(*args, **kwargs) file "c:\python27\lib\site-packages\django\utils\decorators.py" in _wrapped_view 93. response = view_func(request, *args, **kwargs) file "c:\python27\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func 79. response = view_func(request, *args, **kwargs) file "c:\python27\lib\site-packages\django\contrib\admin\sites.py" in inner 197. return view(request, *args, **kwargs) file "c:\python27\lib\site-packages\django\utils\decorators.py" in _wrapper 28. return bound_func(*args, **kwargs) file "c:\python27\lib\site-packages\django\utils\decorators.py" in _wrapped_view 93. response = view_func(request, *args, **kwargs) file "c:\python27\lib\site-packages\django\utils\decorators.py" in bound_func 24. return func(self, *args2, **kwargs2) file "c:\python27\lib\site-packages\django\contrib\admin\options.py" in changelist_view 1079. response = self.response_action(request, queryset=cl.get_query_set()) file "c:\python27\lib\site-packages\django\contrib\admin\options.py" in response_action 836. response = func(self, request, queryset) file "c:\documents , settings\anshul\desktop\online.in\online\..\online\assessment\admin.py" in download_selected_document 82. archive.write(content,name) file "c:\python27\lib\zipfile.py" in write 1031. st = os.stat(filename) exception type: typeerror @ /admin/assessment/userdocuments/ exception value: must encoded string without null bytes, not str
i dont know how should fix this.please help
use
zipfile.zipfile().writestr(archived_name, content_to_be_archived)
instead of
zipfile.zipfile().write(filename_to_load_content_from, archived_name=none)
so quick fix might be
archive.write(content,name) => archive.writestr(name, content)
furthermore, may want check
- stringio instead of tempfile if size of zipped file small
- since httpresponse object file-like object, zip directly
- use xsendfile, or x-accel-redirect in nginx transferring responded file instead of relying on django itself
Comments
Post a Comment