【问题标题】:PDF File generated by POST Request Not OpeningPOST 请求未打开生成的 PDF 文件
【发布时间】:2016-07-11 23:34:58
【问题描述】:

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

$.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();   
        }
}); 

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

%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 端做任何其他事情。

这是我们在 ASP.NET 中用于将 PDF 发送到 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();

一些帮助会很棒...

【问题讨论】:

  • 尝试像这样$.ajax({ url: '&lt;url_to_pdf&gt;', success: function(data) { var blob=new Blob([data]); var link=document.createElement('a'); link.href=window.URL.createObjectURL(blob); link.download="Dossier_"+new Date()+".pdf"; link.click(); } }); 改变你的ajax

标签: jquery asp.net ajax pdf post


【解决方案1】:
  1. 您不能使用 document.write 查看 PDF。
  2. 您从 $.ajax 收到的数据不是二进制的,因此 pdf 数据已损坏。

您可以使用 XMLHttpRequest 将 pdf 数据作为 blob 获取并创建一个 blob url 用于打开窗口。

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);      

【讨论】:

  • 用对象 URL 打开一个窗口在 IE 上不起作用。您应该将 blob 传递给函数“window.navigator.msSaveOrOpenBlob”
【解决方案2】:

您可以使用下面的示例代码从发布请求或网络服务中获取 pdf。

function getDocumentByXMLHttpRequest() {
    //reason for this
    //http://stackoverflow.com/questions/36199155/pdf-file-generated-by-post-request-not-opening
    var header = getHeader();
    var requesturl =  < url rest service url to fetch document >
        var xhr = new XMLHttpRequest();
    xhr.open('GET', requesturl);
    xhr.setRequestHeader(header.key, header.value);
    xhr.responseType = 'arraybuffer';
    xhr.onload = function () {
        if (this.status == 200) {
            onloadDocumentFromContent(this.response);
        } else {
            navigator.notification.alert('Error while requesting pdf with id ' + getDocumentId());

        }
    };
    xhr.send();
}
function onloadDocumentFromContent(pdfraw) {

    var docInitParams = {
        data : pdfraw
    };

    PDFJS.getDocument(docInitParams).then(function (pdfDoc_) {
        pdfDoc = pdfDoc_;

        // Initial/first page rendering

        renderPage(pageNum);
    });

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-07
    • 2014-02-22
    • 1970-01-01
    • 2014-01-07
    相关资源
    最近更新 更多