Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jquery ajax不提供pdf文件_Javascript_Jquery_Ajax_Django_Pdf - Fatal编程技术网

Javascript jquery ajax不提供pdf文件

Javascript jquery ajax不提供pdf文件,javascript,jquery,ajax,django,pdf,Javascript,Jquery,Ajax,Django,Pdf,我正在尝试使用jquery、ajax和django下载一个pdf文件 My 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('shippi

我正在尝试使用jquery、ajax和django下载一个pdf文件

My 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
...
错误:无效的外部参照流标题
...
警告:索引所有PDF对象
pdf.worker.js(第235行)
PDF 85b859244e496561d60d217869d5d38a[1.3-/-](PDF.js:1.3.76)
错误:扁平流中的FCHECK错误:120239
...

是完整的日志文件。

我不是jQuery专家,但我认为jQuery ajax不支持blob。在文档中,它只列出了以下数据类型:xml、json、脚本或html。 但是,我能够在不使用jQuery和ajax的情况下使用此代码实现此功能

我的JavaScript(我还将添加错误处理)

我的django视图(我还将为此添加错误处理)

此外,如果您不需要在新选项卡中打开,只需下载文件,那么您可以完全避免ajax/Javascript,只需使用HTML,这是一种更简单的方法

为了获得信任和进一步阅读,我使用了这些链接


我不是jQuery专家,但我认为jQueryAjax不支持blob。在文档中,它只列出了以下数据类型:xml、json、脚本或html。 但是,我能够在不使用jQuery和ajax的情况下使用此代码实现此功能

我的JavaScript(我还将添加错误处理)

我的django视图(我还将为此添加错误处理)

此外,如果您不需要在新选项卡中打开,只需下载文件,那么您可以完全避免ajax/Javascript,只需使用HTML,这是一种更简单的方法

为了获得信任和进一步阅读,我使用了这些链接

var xhr = new XMLHttpRequest();
xhr.open('GET', '/pdf_test', true);
xhr.responseType = 'blob';

xhr.onload = function(e) {
  if (this.status == 200) {
    var blob = new Blob([this.response], {type: 'application/pdf'}),
    fileURL = URL.createObjectURL(blob);
    window.open(fileURL,'_blank');
  }
};

xhr.send();
def pdf_test(request):
    pdf_file = open(r'C:\Pdfs\calculation_of_semiconductor_failure_rates.pdf', 'rb')
    response = HttpResponse(pdf_file, content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="shippinglabels.pdf"'
    return response