javascript - Nginx“X-Accel-Redirect”以明文形式提供pdf
问题描述
我在使用 .pdf 文件在浏览器上显示时遇到问题X-Accel-Redirect
。直接从 Nginx 公共位置链接文件 URL 效果很好。X-Accel-Redirect
但是,通过添加“内部”并从 Django调用来限制对该位置的访问,HttpResponse()
将 pdf 文件作为纯文本发送,就像任何其他静态文件(css、js、html)一样。
蟒蛇响应
response = HttpResponse()
response['Content-Type'] = 'application/pdf'
response['Content-Disposition'] = 'attachment; filename=example.pdf'
response['X-Accel-Redirect'] = '/media/file-pdf/example.pdf'
return response
Nginx 位置
location /media/{
internal;
alias /var/www/media/;
default_type application/pdf;
}
Javascript
$.ajax({
url: http://www.example.com/pdf-api/,
type: 'GET',
success: function(data) {
window.open(data);
console.log(data);
},
});
接收到的文本形式的示例 pdf(如在浏览器控制台上所见)
%PDF-1.3
%“Œ‹ž ReportLab Generated PDF document http://www.reportlab.com
1 0 obj
<<
/F1 2 0 R
>>
endobj
2 0 obj
<<
/BaseFont /Helvetica /Encoding /WinAnsiEncoding /Name /F1 /Subtype /Type1 /Type /Font
>>
endobj
3 0 obj
<<
/Contents 7 0 R /MediaBox [ 0 0 595.2756 841.8898 ] /Parent 6 0 R /Resources <<
/Font 1 0 R /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ]
>> /Rotate 0 /Trans <<
>>
/Type /Page
>>
endobj
4 0 obj
<<
/PageMode /UseNone /Pages 6 0 R /Type /Catalog
>>
endobj
5 0 obj
<<
/Author (anonymous) /CreationDate (D:20180925202407-08'00') /Creator (ReportLab PDF Library - www.reportlab.com) /Keywords () /ModDate (D:20180925202407-08'00') /Producer (ReportLab PDF Library - www.reportlab.com)
/Subject (unspecified) /Title (untitled) /Trapped /False
>>
endobj
6 0 obj
<<
/Count 1 /Kids [ 3 0 R ] /Type /Pages
>>
endobj
7 0 obj
<<
/Filter [ /ASCII85Decode /FlateDecode ] /Length 856
>>
我的问题是:向使用 Python + Django + Nginx 的用户提供受限 pdf(或任何文件)的正确方法是什么?或者我应该在浏览器端将发送的二进制数据转换为pdf?
解决方案
推荐阅读
- nginx - Nginx反向代理后面的Qtorrent Web GUI未加载登录网页css
- ios - 填充集合视图内的图像视图
- python-3.x - PYQT Selected Combobox 项目应删除另一个组合框中的项目
- visual-studio - 没有匹配的配置文件 [对于转移的应用程序]
- airflow - 如何将 BigQueryOperator 与 execution_date 一起使用?
- java - Tomcat 停止问题 + Spring Boot + 服务
- python - 使用 facebook 先知在具有多个时间序列的数据框中进行时间序列预测
- excel - 我需要在 vba 中使用后期绑定来获取当前活动的 Word 文档名称
- hyperledger-fabric - MSP 错误:提供的身份无效:x509:证书由未知机构签名
- json - How to get the HostedZoneId of a HostedZone in As route53 with same name?