使用Pyramid,ningx,X-Accel-Redirect标头提供pdf下载 [英] 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>
拥有一个金字塔1.2.7应用程序来处理这样的视图
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
我的nginx配置看起来像这样
And my nginx configuration looks like this
location /path/ {
internal;
root /opt/tmp;
}
这一切正常,但是浏览器显示了一堆PDF垃圾,而不是显示pdf的浏览器已下载.
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?
推荐答案
如果要指示Web浏览器应该下载资源而不是显示资源,请尝试使用Content-Disposition
标头
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:
Content-Disposition: attachment
您还可以通过此标头为下载的文件指定文件名(如果它与URL中的最后一个路径部分不同):
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):
Content-Disposition: attachment; filename=foo.pdf
查看 Nginx文档,此响应标头应与X-Accel-Redirect
功能一起正常使用您正在使用.
Looking at the Nginx documentation, this response header should work correctly in conjunction with the X-Accel-Redirect
feature you're using.
这篇关于使用Pyramid,ningx,X-Accel-Redirect标头提供pdf下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!