简体   繁体   English

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

[英]Django download files errors “zip file is invalid”

Here is my view code to download a file 这是我下载文件的视图代码

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

I have a model that has a file field named "order". 我有一个具有名为“ order”的文件字段的模型。 What am i doing wrong here because when I click on the link. 我在这里做错了什么,因为当我单击链接时。 I get the download file but when I try to open it I get "The compressed zip folder is invalid". 我得到下载文件,但是当我尝试打开它时,得到“压缩的zip文件夹无效”。 Please help. 请帮忙。 Thanks 谢谢

You should understand that this code only sends the file from order.path , you need to create that file in your code. 您应该了解,此代码仅从order.path发送文件,您需要在代码中创建该文件。 Regarding creating zip files you can start with the stdlib zipfile module. 关于创建zip文件,您可以从stdlib zipfile模块开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM