GET Ajaxリクエストがあり、application / pdf形式でコード化されたコンテンツが返されます。
たとえば、私のリクエストは次のタイプの文字列を返します。
%PDF-1.4%����4 0 obj <> stream
x��endstream endobj 6 0 obj <> streamx���]o�0���+�e{Q�$��R�N�������@-
���n��wBL $ .JDL |�>獝��[�
�> {���= _ʻ(寒����1`ؚyW��G���XϽo�1�6��Ӳ.�4(u�C��8�j���H� l�����f6*k�Fa^�e�G=��J0��}
7L�z�ͺ��p�H����&�ɴ�L�Z�
レンダリングがおかしい特殊文字を失礼します。問題はそれだけです。私のリクエストから取得したこの応答を受け取り、このコンテンツを別のブラウザータブで正しく印刷したいと思います。これを行うためのコードを作成しました。Blobオブジェクトを使用しました。 「PDFを読む」のページが正しく開きます。しかし、完全に白いPDF文書のみが表示されます。これは、作成した文書に私の回答が書かれていなかったことを示しています。
以下に、私のリクエストのコードを示します。
$.ajax({
type: "GET",
url:urll + parameter,
async: true,
crossDomain: true,
accept: {
pdf: 'application/pdf'
},
success:function(response, status, xhr){
alert("Response: "+response);
alert("Status: "+status);
alert("Xhr: "+xhr);
// check for a filename
var filename = "";
var disposition = xhr.getResponseHeader('Content-Disposition');
if (disposition && disposition.indexOf('attachment') !== -1) {
var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
var matches = filenameRegex.exec(disposition);
if (matches != null && matches[1]) filename = matches[1].replace(/['"]/g, '');
}
var type = xhr.getResponseHeader('Content-Type');
var blob = new Blob([response], { type: type });
if (typeof window.navigator.msSaveBlob !== 'undefined') {
// IE workaround for "HTML7007: One or more blob URLs were revoked by closing the blob for which they were created. These URLs will no longer resolve as the data backing the URL has been freed."
window.navigator.msSaveBlob(blob, filename);
} else {
var URL = window.URL || window.webkitURL;
var downloadUrl = URL.createObjectURL(blob);
if (filename) {
// use HTML5 a[download] attribute to specify filename
var a = document.createElement("a");
// safari doesn't support this yet
if (typeof a.download === 'undefined') {
//window.location = downloadUrl;
window.open(downloadUrl, '_blank');
} else {
a.href = downloadUrl;
a.download = filename;
document.body.appendChild(a);
a.setAttribute("target","_blank");
a.click();
}
} else {
//window.location = downloadUrl;
window.open(downloadUrl, '_blank');
}
setTimeout(function () { URL.revokeObjectURL(downloadUrl); }, 100); // cleanup
}
},
error: function(xhr,status,error){
alert("error "+xhr+" "+error+" "+status);
}
});
私が理解しているように、GETを使用していて、新しいタブにPDFファイルが必要です。
Soo ...なぜこのような新しいウィンドウでこのURLを開いてみませんか?
var fullUrl = urll + parameter;
window.open(fullUrl);