POST请求未打开生成的PDF文件

时间:2021-06-27 09:46:48

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

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

$.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...

The response header is

响应头是

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

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

在发出POST请求时,我有什么问题吗?或者我们是否需要在API方面做任何其他事情。

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

这是我们在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();

Some help would be great...

一些帮助会很棒......

2 个解决方案

#1


2  

  1. You cannot use document.write to view a PDF.
  2. 您无法使用document.write查看PDF。

  3. The data you receive from $.ajax isn't binary so the pdf data is corrupt.
  4. 您从$ .ajax收到的数据不是二进制文件,因此pdf数据已损坏。

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

您可以使用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);      

#2


0  

you can use below sample code to get pdf from a post request or webservice.

您可以使用以下示例代码从post请求或webservice获取pdf。

function getDocumentByXMLHttpRequest() {
    //reason for this
    //http://*.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);
    });

}

#1


2  

  1. You cannot use document.write to view a PDF.
  2. 您无法使用document.write查看PDF。

  3. The data you receive from $.ajax isn't binary so the pdf data is corrupt.
  4. 您从$ .ajax收到的数据不是二进制文件,因此pdf数据已损坏。

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

您可以使用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);      

#2


0  

you can use below sample code to get pdf from a post request or webservice.

您可以使用以下示例代码从post请求或webservice获取pdf。

function getDocumentByXMLHttpRequest() {
    //reason for this
    //http://*.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);
    });

}