0

First of all I want to say that I know its bad to serve files from django but my situation can only be handled by django so I chose it to serve zipped files.In my models.py I have a model

class Documents(models.Model):
    filename = models.CharField(max_length=100)
    document = models.FileField(upload_to='docs')
    allowedGroup = models.ManyToManyField(Group)

So when a normal user log-in it will be displayed the Documents which he has permission according to his/her group.I want user to be able to download multiple Documents(files) at one go.So what I did is added this handler for downloading multiple files as a zip file:

I used this Django Snippet for creating this view

  def Download_selected_document(self, request, queryset):  

    if len(queryset)>1:

        temp = tempfile.TemporaryFile()
        archive = zipfile.ZipFile(temp, 'w', zipfile.ZIP_DEFLATED)
        for i in queryset:
            ##Reading the file content
            content = open(settings.MEDIA_ROOT+str(i.document),'rb').read()
            ##name is name of file like "abc.docx"
            name = str(queryset[0].document)[10:]
            ##At this like it 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 for downloading.")

Error I got is : must be 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 and 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 at /admin/assessment/userdocuments/
Exception Value: must be encoded string without NULL bytes, not str

I dont know how should I fix this.Please help

Anshul
  • 7,914
  • 12
  • 42
  • 65

1 Answers1

3

Use

zipfile.ZipFile().writestr(archived_name, content_to_be_archived)

instead of

zipfile.ZipFile().write(filename_to_load_content_from, archived_name=None) 

So a quick fix might be

archive.write(content,name) => archive.writestr(name, content)

Furthermore, you may want to check

  • StringIO instead of tempfile if the size of zipped file is normally small
  • Since HttpResponse object is a file-like object, you could zip to it directly
  • Use XSendfile, or X-Accel-Redirect in nginx to help transferring the responded file instead of relying on Django itself
okm
  • 23,575
  • 5
  • 83
  • 90
  • Awesome.........it worked for me ,but its should be archive.write(content,name) => archive.writestr(name, content) – Anshul Mar 10 '12 at 08:59
  • 1
    @anks aha, yes. too many WRITEs here confused me also =p fixed – okm Mar 10 '12 at 09:22