【发布时间】:2016-10-22 21:25:30
【问题描述】:
我正在尝试使用 jquery、ajax 和 django 下载 pdf 文件。
我的 django views.py:
if request.POST.get('action') == 'download_labels':
order_list = json.loads(request.POST.get('order_dict'), None)
PackedOrders(dbname, order_list).downloadLabels()
file = open('shipping_labels.pdf','rb')
response = HttpResponse(file, content_type='application/pdf')
response['Content-Disposition'] = "attachment; filename=shipping_labels.pdf"
os.system('rm shipping_labels.pdf')
return HttpResponse(response, content_type='application/pdf')
我的 ajax 查询:
data : {action:'download_labels',
order_dict:JSON.stringify($checkedRows)},
success : function(response, status, request) {
var file = new Blob([response], {type: 'application/pdf'});
var fileURL = window.URL.createObjectURL(file);
window.open(fileURL,'_blank');
},
ajax 将文件作为二进制数据响应返回并在新选项卡中打开它。但我在新标签中看到的只是空白页。空白页数等于原始pdf文件的页数。
在控制台中我看到了这个:
Error: Invalid XRef stream header
...
Warning: Indexing all PDF objects
pdf.worker.js (line 235)
<System>
PDF 85b859244e496561d60d217869d5d38a [1.3 - / -] (PDF.js: 1.3.76)
Error: Bad FCHECK in flate stream: 120, 239
...
Here 是完整的日志文件。
【问题讨论】:
标签: javascript jquery ajax django pdf