[英]Django ZIP multiple images for download
I'm trying to zip several images in a ZIP file.我正在尝试将多个图像压缩到一个 ZIP 文件中。
Although It's a Django app, files are not stored in the same app.虽然它是一个 Django 应用程序,但文件并不存储在同一个应用程序中。 I want to fetch them from a url list.
我想从 url 列表中获取它们。
I get the following error: FileNotFoundError [Errno 2] No such file or directory: 'image1.jpg'
我收到以下错误:
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
I reviewed several posts and finaly followed this one how can I export multiple images using zipfile and urllib2 - django我查看了几篇文章,最后关注了这篇文章如何使用 zipfile 和 urllib2 - django 导出多个图像
But I can't get this working.但我不能让它工作。 Any clues welcome.
欢迎任何线索。 Thanks in advance.
提前致谢。
Per the zip
documentation , write()
is used to write an existing file into the filesystem (by path).根据
zip
文档, write()
用于将现有文件写入文件系统(按路径)。 I think that you are looking for writestr()
, which can be passed the actual file contents.我认为您正在寻找
writestr()
,它可以传递实际的文件内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.