Reputation: 909
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?
Upvotes: 1
Views: 2248
Reputation: 635
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!'); });
Upvotes: 1