Skip to content Skip to sidebar Skip to footer

Django Download Images In Zip File

I use this code in DJANGO framework to can some user download images. this code work fine and download image every time to press download some user. but this code download absolute

Solution 1:

Try the following:

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

Post a Comment for "Django Download Images In Zip File"