S
S
stayHARD2016-01-28 13:48:06
Django
stayHARD, 2016-01-28 13:48:06

How to return a file for download from Django?

Hello.
There is a .zip archive in a certain directory, you need to return the file for download when you access the link through the blizzard.
I do this:

def download_file(request):
    response = HttpResponse(open("test.zip"), content_type='application/zip')
    response['Content-Disposition'] = 'attachment; filename=test.zip'
    response['Content-Type'] = 'application/x-zip'
    return response

I get a folder from the archive. I want to get the same .zip archive.
Version - Django 1.9.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2016-01-28
@sim3x

django x-accel-redirect
stackoverflow.com/questions/28704712/django-nginx-...

server {
    listen       27796;
    server_name  myurl.com;
    root /home/ucwsri/webapps/static_media_ucwsri_nginx; 

    location / {
        autoindex on;
    }

    location ^~ /protected/ {
        internal;
    }

response = HttpResponse()
url = "/protected/some-file.pdf"
response['X-Accel-Redirect'] = url

return response

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question