django / python |从数据库中获取文件,压缩,然后发送到浏览器

时间:2011-09-05 11:11:18

标签: python django

请帮我解决我的问题。

如果这听起来很棒,我很抱歉,因为我真的很喜欢。某个数据库中有文件(jpg,docs),我如何在数据库中选择这些文件,将它们保存在zip文件中,然后以HttpResponse或类似的方式发送到浏览器:

response = HttpResponse(mimetype='application/zip')
response['Content-Disposition'] = 'filename=group.zip'

困扰我的部分是从数据库中获取文件时,谷歌似乎没有帮助。

我所拥有的一些相关代码是:

temp = request.POST.getlist('cbox') #cbox is the field in form
tempx = models.FileTable.objects.filter(pk__in = temp)

我不再知道该怎么做了。提前致谢! :P

1 个答案:

答案 0 :(得分:1)

您正在寻找zipfileStringIO。创建StringIO,将其打开为ZipFile,向其中添加文件,然后send it