【发布时间】:2017-11-13 12:23:05
【问题描述】:
我在 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 文件中的图像是否容易?
【问题讨论】:
-
您可以使用压缩文件的python库,然后将该文件作为HttpResponse返回:)
-
@mohammedqudah 怎么样?第一次使用下载
标签: python django python-2.7