[英]Serve up pdf as a download with Pyramid, ningx, X-Accel-Redirect Header
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 有一个金字塔1.2.7应用程序处理这样的视图
@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 我的nginx配置看起来像这样
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. 这一切都有效,但浏览器显示pdf已下载,而不是浏览器显示一堆PDF垃圾。
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 . 如果要指示Web浏览器应下载资源而不是显示它,请尝试使用RFC 6266中所述的
Content-Disposition
标头。 For example, the following response header will tell the browser to download the file: 例如,以下响应标头将告诉浏览器下载文件:
Content-Disposition: attachment
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): 您还可以通过此标头为下载的文件指定文件名(如果它与URL中的最后一个路径组件不同):
Content-Disposition: attachment; filename=foo.pdf
Looking at the Nginx documentation , this response header should work correctly in conjunction with the X-Accel-Redirect
feature you're using. 查看Nginx文档 ,此响应标头应与您正在使用的
X-Accel-Redirect
功能一起正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.