Django ZIP多张图片供下载

2024-06-25 23:03:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将多个图像压缩到一个压缩文件中

虽然它是一个Django应用程序,但文件并不存储在同一个应用程序中。我想从url列表中获取它们

我得到以下错误:FileNotFoundError [Errno 2] No such file or directory: 'image1.jpg'

def download_image(request):
    fotos = ['https://storage.googleapis.com/some/image1.jpg', 'https://storage.googleapis.com/some/image2.jpg']

    f = StringIO()
    zip = ZipFile(f, 'w')

    for foto in fotos:
        url = urlopen(foto)
        filename = str(foto).split('/')[-1]
        zip.write(filename, url.read())

    zip.close()
    response = HttpResponse(f.getvalue(), content_type="application/zip")
    response['Content-Disposition'] = 'attachment; filename=image-test.zip'

    return response

我看了几篇文章,最后看了这篇how can I export multiple images using zipfile and urllib2 - django

但我不能让它工作。欢迎提供任何线索。提前谢谢


Tags: httpsimagecom应用程序urlresponsestoragesome