2012-03-10 42 views
0

首先,我想說我知道它從django提供的文件不好,但我的情況只能由django處理,所以我選擇它來提供壓縮文件。 PY我有一個模型在Django視圖中壓縮文件併爲它們提供服務

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

因此,當一個普通用戶登錄它會顯示這是他根據他/她的group.I希望用戶能夠下載多個文件有權限的文件(文件)所以我所做的就是添加這個處理程序來下載多個文件作爲zip文件:

我用這個Django Snippet來創建這個視圖

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.") 

錯誤我得到的是:必須在沒有空字節編碼字符串,而不是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 

我不知道我應該如何解決this.Please幫助

回答

3

使用

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

而不是

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

所以速戰速決可能是

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

此外,你可能要檢查

  • StringIO的的臨時文件,而不是如果壓縮文件大小通常很小
  • 由於HttpResponse對象對象是類似文件的對象,可以直接壓縮到該對象
  • 在nginx中使用XSendfile或X-Accel-Redirect來幫助轉換而不是依靠Django本身
+0

太棒了.........它爲我工作,但它應該是archive.write(content,name)=> archive.writestr(name,內容) – Anshul 2012-03-10 08:59:05

+1

@anks aha,是的。太多的WRITE在這裏讓我困惑,也是= p固定 – okm 2012-03-10 09:22:10

相關問題