Django下载文件错误“zip文件无效”

时间:2013-03-22 20:50:20

标签: python django file web-applications

这是我下载文件的视图代码

def download_order(request,order_id):
      purchase=GigPurchase.objects.select_related().get(order_id=order_id)
      order=purchase.order  
      wrapper=FileWrapper(open(order.path,"rb")) 
      content=mimetypes.guess_type(order.path)[0]
      t=purchase.gig.title
      title=slugify(t)
      response=HttpResponse(wrapper,content_type=content)
      response['Content-Disposition']='attachment;filename=%s.zip'%(title)
      return response

我的模型有一个名为“order”的文件字段。我在这里做错了什么,因为当我点击链接时。我得到了下载文件但是当我尝试打开它时,我得到“压缩的zip文件夹无效”。请帮忙。感谢

1 个答案:

答案 0 :(得分:0)

您应该了解此代码仅从order.path发送文件,您需要在代码中创建该文件。关于创建zip文件,您可以从stdlib zipfile模块开始。