私は初心者です。ほとんどのコードはgoogle検索からです。私は私のpdfのダウンロードを下のコードで試してみました(試行錯誤のプレイ)。上記のコードヒント(xhrFields)をありがとうございます。 jQuery.ajax ``のためXMLHttpRequest`を代入
$.ajax({
cache: false,
type: 'POST',
url: 'yourURL'
contentType: false,
processData: false,
data: yourdata,
//xhrFields is what did the trick to read the blob to pdf
xhrFields: {
responseType: 'blob'
},
success: function (response, status, xhr) {
var filename = "";
var disposition = xhr.getResponseHeader('Content-Disposition');
if (disposition) {
var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
var matches = filenameRegex.exec(disposition);
if (matches !== null && matches[1]) filename = matches[1].replace(/['"]/g, '');
}
var linkelem = document.createElement('a');
try {
var blob = new Blob([response], { type: 'application/octet-stream' });
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;
} else {
a.href = downloadUrl;
a.download = filename;
document.body.appendChild(a);
a.target = "_blank";
a.click();
}
} else {
window.location = downloadUrl;
}
}
} catch (ex) {
console.log(ex);
}
}
});
てみてください() 'を参照http://stackoverflow.com/questions/12876000/how-to-build-pdf-file-from-binary-string-returned- Webサービスを使用したWebサービスから – guest271314
なぜこれのためにAJAXを使用する必要がありますか?ファイルをダウンロードすることなく、ファイルをダウンロードすることなく、はるかに簡単かつ確実にファイルをダウンロードできます。 –
あなたは 'json'レスポンスでpdfを作ることはできません。これは' HTML'レスポンスでなければならず、すべてのブラウザで動作しません。与えられたex。 'Url'はHTML応答を意味します。 –