django HttpResponse下载多张图片
时间: 2023-11-30 13:02:38 浏览: 133
可以使用zipfile模块将多张图片压缩成一个zip文件,然后使用HttpResponse返回压缩文件。具体代码如下:
```python
import zipfile
from django.http import HttpResponse
def download_images(request):
# image_paths为图片路径列表
image_paths = ['/path/to/image1.jpg', '/path/to/image2.jpg', '/path/to/image3.jpg']
# 创建zip文件并添加图片
zip_file = zipfile.ZipFile('images.zip', 'w')
for image_path in image_paths:
zip_file.write(image_path)
zip_file.close()
# 返回HttpResponse
with open('images.zip', 'rb') as f:
response = HttpResponse(f.read(), content_type='application/zip')
response['Content-Disposition'] = 'attachment; filename=images.zip'
return response
```
注意:在返回HttpResponse时,需要设置Content-Disposition头部,指定文件名和下载方式。
阅读全文