How to return multiple files in HttpResponse Django

后端 未结 1 1116
既然无缘
既然无缘 2021-01-06 12:32

I have been wracking my brains in this problem. Is there a way in django to serve multiple files from a single HttpResponse?

I have a scenario where i am looping thr

1条回答
  •  攒了一身酷
    2021-01-06 13:19

    Maybe if you try to pack all files in one zip you can archive this in Admin

    Something like:

        def zipFiles(files):
            outfile = StringIO()  # io.BytesIO() for python 3
            with zipfile.ZipFile(outfile, 'w') as zf:
                for n, f in enumerate(files):
                    zf.writestr("{}.csv".format(n), f.getvalue())
            return outfile.getvalue()
    
        zipped_file = zip_files(myfiles)
        response = HttpResponse(zipped_file, content_type='application/octet-stream')
        response['Content-Disposition'] = 'attachment; filename=my_file.zip'
    

    0 讨论(0)
提交回复
热议问题