[英]How to download a archive using django response?
I want to download the archive after i created.我想在创建后下载存档。 The code is bellow:
代码如下:
def downloadArchive(room, catalog):
test = RoomTest.objects.get(idRoom=room)
zip_name = room.name + "_" + token_urlsafe(5) + '.zip'
zip_path = 'media/zipFiles/' + zip_name
if test.OLD_Archive != '' and os.path.exists(test.OLD_Archive): # remove old file.
os.remove(test.OLD_Archive)
with ZipFile(zip_path, 'w') as zipObj:
for student in catalog:
zipObj.write('media/' + str(student.file),
student.studentFullName() + os.path.splitext('media/' + str(student.file))[1])
test.OLD_Archive = zip_path
test.save()
response = HttpResponse(zipObj)
response['Content-Type'] = 'application/x-zip-compressed'
response['Content-Disposition'] = 'attachment; filename=' + zip_name
return response
Every time i try to download, got error "The archive is either in unknown format or damaged".每次我尝试下载时,都会出现错误“存档格式未知或已损坏”。 I think the route is broken.
我觉得这条路坏了。 Any ideas how can i check the response route?
任何想法如何检查响应路线?
Btw: if i download the archive manually (after archive), it's working fine.顺便说一句:如果我手动下载存档(存档后),它工作正常。
Solved, i used:解决了,我用过:
response = redirect('/' + zip_path)
return response
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.