我正在向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();
}
});
成功后我在新窗口中打开响应数据......但我得到以下输出......
%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();
一些帮助会很棒......
答案 0 :(得分:2)
您可以使用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);
答案 1 :(得分:0)
您可以使用以下示例代码从帖子请求或网络服务获取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);
});
}