1

I want to make a response that both redirect to certain url and donwload a file. To download a file I use:

    content = "Example content"
    filename = "example-file-name".
    response = HttpResponse(content=content,
                                    content_type='text/plain')
    response['Content-Disposition'] = 'attachment; filename={0}'.format(filename)
    return response

To redirect to url:

 response = HttpResponseRedirect(redirect_to=example_url)

Is there a way to make both things in a single response?

Davit Tovmasyan
  • 3,238
  • 2
  • 20
  • 36
  • Unfortunately, this is not possible, you can only return one http response at a time. You can do it the other way around though, redirect and then download the file or use AJAX. – Isma May 30 '18 at 12:40

1 Answers1

1

Going AJAX might be a solution, download the file via AJAX, then initiate redirection after download complete.

AJAX download can be eased using JQuery or related plugins, a post for reference.

Redirection can be achieved on success at client side like below.

 $.fileDownload('some/file.pdf')
    .done(function () { window.location.href = 'REDIRECT_URL'; })
    .fail(function () { alert('File download failed!'); });
Vinay P
  • 617
  • 3
  • 13