I want a user to be able to click a link like this:
<a href="/download?file=123">download</a>
Have a Pyramid 1.2.7 app handle the view like this
@view_config(route_name='download')
def download(request):
file_id = request.GET['file']
filename = get_filename(file_id)
headers = request.response.headers
headers['Content-Description'] = 'File Transfer'
headers['Content-Type'] = 'application/force-download'
headers['Accept-Ranges'] = 'bytes'
headers['X-Accel-Redirect'] = ("/path/" + filename + ".pdf")
return request.response
And my nginx configuration looks like this
location /path/ {
internal;
root /opt/tmp;
}
This all works but instead of the browser showing a pdf has download, the browser displays a bunch of PDF garbage.
How do I setup my Pyramid view to get the browser to do the right thing?
If you want to indicate that a web browser should download a resource rather than display it, try using the
Content-Disposition
header as described in RFC 6266. For example, the following response header will tell the browser to download the file:You can also specify a file name for the downloaded file through this header (if it differs from the last path component in the URL):
Looking at the Nginx documentation, this response header should work correctly in conjunction with the
X-Accel-Redirect
feature you're using.