在Django管理员操作中下载上传的文件

问题描述

我正在尝试在Django中创建一个管理员操作,以下载用户上传的所选文件。我可以一张一张地查看上传的文件,并使用保存链接来保存文件/图像。但是我想通过选择多个下载并将它们保存到一个zip文件夹中,以便一次性下载。 我将此download users' pdf in django用作参考,并创建了以下代码。我可以下载一个zip文件,但是当我打开它时,它说该zip文件无效。我认为查询文件网址/将其写入zip文件夹存在问题。请协助。

import tempfile
import zipfile

def downloadPic(self,request,queryset):
    with tempfile.SpooledTemporaryFile() as tmp:
        with zipfile.ZipFile(tmp,'w',zipfile.ZIP_DEFLATED) as archive:
            for index,item in enumerate(queryset):
                projectUrl = str(item.file.url) + ''
                fileNameInZip = '%s.zip' % index
                archive.writestr(fileNameInZip,projectUrl)
            tmp.seek(0)
            response = HttpResponse(tmp.read(),content_type='application/x-zip-compressed')
            response['Content-Disposition'] = 'attachment; filename="pictures.zip"'
            return response

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...