微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python – 使用Pyramid,ningx,X-Accel-Redirect Header下载pdf作为下载

我希望用户能够点击这样的链接

< a href =“/ download?file = 123”> download< / a>

一个金字塔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

我的Nginx配置看起来像这样

location /path/ {
 internal;
 root /opt/tmp; 
}

这一切都有效,但浏览器显示pdf已下载,而不是浏览器显示一堆PDF垃圾.

如何设置我的金字塔视图以使浏览器做正确的事情?

解决方法:

如果要指示Web浏览器应该下载资源而不是显示它,请尝试使用Content-disposition标头as described in RFC 6266.例如,以下响应标头将告诉浏览器下载该文件

Content-disposition: attachment

您还可以通过此标头为下载的文件指定文件名(如果它与URL中的最后一个路径组件不同):

Content-disposition: attachment; filename=foo.pdf

查看Nginx documentation,此响应标头应与您正在使用的x-accel-redirect功能一起正常工作.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐