[英]django download images in zip file
我在DJANGO框架中使用此代码可以使一些用户下载图像。 此代码可以正常工作,并且每次按下载某些用户时下载图像。
但是此代码下载的是绝对图片,我需要将这些图片压缩为任何用户下载的内容。
def download_image(request, id):
product_image=MyModel.objects.get(pk=id)
product_image_url = product_image.upload.url
wrapper = FileWrapper(open(settings.MEDIA_ROOT+ product_image_url[6:], 'rb'))
content_type = mimetypes.guess_type(product_image_url)[0]
response = HttpResponse(wrapper, content_type=content_type)
response['Content-Disposition'] = "attachment; filename=%s" % product_image_url
return response
更改此代码以下载zip文件中的图像很容易吗?
请尝试以下操作:
def download_image(request, id):
product_image=MyModel.objects.get(pk=id)
product_image_url = product_image.upload.url
image_path = settings.MEDIA_ROOT+ product_image_url[6:]
image_name = 'whatevername.png'; # Get your file name here.
with ZipFile('export.zip', 'w') as export_zip:
export_zip.write(image_path, image_name)
wrapper = FileWrapper(open('export.zip', 'rb'))
content_type = 'application/zip'
content_disposition = 'attachment; filename=export.zip'
response = HttpResponse(wrapper, content_type=content_type)
response['Content-Disposition'] = content_disposition
return response
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.