POST请求未打开生成的PDF文件 [英] PDF File generated by POST Request Not Opening

查看:155
本文介绍了POST请求未打开生成的PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向应该提供PDF文件的API URL发送POST请求.

I am sending a POST request to an API URL which is supposed to give the PDF file.

$.ajax({
  url: "http://ourDevEnv.com:5000/api/v1/Docs/Process",
    type: "POST", data: printData,
    crossDomain: true,  
    contentType: "application/json; charset=UTF-8",
        success : function(data) {
            var WinId = window.open('', 'newwin', 'width=600,height=800');
            WinId.document.open();
            WinId.document.write(data);
            WinId.document.close();   
        }
}); 

成功后,我将在新窗口中打开响应数据...但是我得到以下输出...

On success I am opening the response data in a new window... But I am getting the following output...

%PDF-1.6
%����
5 0 obj
<</Length 2042/Filter/FlateDecode>>stream
x����o5�-���$�@Aڇ4M�Y����&-�h�����V��R�¿��c���y�kJ{�w�����3�c߽_~{����n��-���Ywp�\6�7׷ˣ����A����z�z�~�$�F4��Tچ1AE�0E�i>���Y����x�F��1Xa��k�n�m9�Ds�'؂7[o�\�}���|I�"w�x�,�G䀜�W��k��uj��;ʻƘ�2Q*���%Uv[�%o�9y�aKr�x#����s
ICm#:��[��B#�p��������'��-��1mm�|��
M����3�!���K0�)<�B������ߴ�C�  ���CP�����Sm{�VR�f[p:N[nɏ��wA����+Hh!oq��
V˙�Ԩm5n�/��y���?F!ح�B��g0�w�]���Sn   �5݃ǻ����Mh    �V�%�k��V~���>�/�{o|P.p��P�v�v:tK%ۖ�@�?�����@��kjLު��NS(�X7��z�Ru���a��(TĒL�7K��-�ʄt��M(���)y9�@�E��;VX���}�Eӡ!7�����)֥
�a�
more text like this...

响应头是

{
  "access-control-allow-origin": "*",
  "date": "Thu, 24 Mar 2016 09:18:28 GMT",
  "server": "Kestrel",
  "transfer-encoding": "chunked",
  "content-type": "application/pdf"
}

发出POST请求时,我做错了什么吗?还是我们需要在API方面做其他事情.

Is there anything I am doing wrong while making the POST request? or do we need to do anything else on the API side.

这是我们在ASP.NET中用于将PDF发送到REST的代码...

This is the code we use in ASP.NET to send PDF to the REST...

    Page.Response.Buffer = true;
    Page.Response.ClearContent();
    Page.Response.ClearHeaders(); 

    Document doc = reportGenerator.RenderDocument();
    Stream m = Response.OutputStream;

    PdfExportFilter pdf = new PdfExportFilter();
    pdf.KeepOriginalImageResolutionAndQuality = true;
    pdf.ImageQuality = 200;
    pdf.Export(doc, m); 

    Page.Response.ContentType = "application/pdf";
    Page.Response.End();

一些帮助会很棒...

Some help would be great...

推荐答案

  1. 您不能使用document.write查看PDF.
  2. 您从$ .ajax接收的数据不是二进制数据,因此pdf数据已损坏.

您可以使用XMLHttpRequest将pdf数据作为blob获取,并创建用于打开窗口的blob网址.

You can use XMLHttpRequest to get the pdf data as a blob and create a blob url to use to open the window.

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
    if (this.readyState == 4 && this.status == 200){
        var url = window.URL || window.webkitURL;
        var WinId = window.open(url.createObjectURL(this.response);, 'newwin', 'width=600,height=800');
    }
}
xhr.open('POST', 'http://ourDevEnv.com:5000/api/v1/Docs/Process');
xhr.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
xhr.responseType = 'blob';
xhr.send(printData);      

这篇关于POST请求未打开生成的PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆