我在DJANGO框架中使用此代码可以让一些用户下载图片。此代码运行良好,每次按download某些用户时都会下载图像。
但此代码下载绝对图像我需要为任何用户下载压缩此图像。
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文件吗?
发布于 2017-11-13 21:12:41
尝试以下操作:
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
https://stackoverflow.com/questions/47264298
复制相似问题