Here is the code for my JQuery Ajax POST request:
function LoadPDF() {
var parameters = [];
var request = $.ajax({
type: "POST",
url: "DirectoryQuery.aspx/GetFullPdf",
contentType: "application/json; charset=UTF-8",
responseType: "text/plain; charset=UTF-8",
async: true,
dataType: "json",
processData: false,
success: handleSuccess,
error: handleError
});
function handleSuccess(data) {
var pdfContent = new Uint8Array(data.d);
saveFile("document.pdf", pdfContent);
}
function handleError(data) {
console.log(data);
//location.reload();
}
}
You can try using file-save-as.js for saving files like below:
function saveFile(fileName, content) {
/* Saves a text string as a blob file */
var ieBrowser = navigator.userAgent.match(/MSIE\s([\d.]+)/),
ie11Browser = navigator.userAgent.match(/Trident\/7.0/) && navigator.userAgent.match(/rv:11/),
edgeBrowser = navigator.userAgent.match(/Edge/g),
browserVersion = (ieBrowser ? ieBrowser[1] : (ie11Browser ? 11 : (edgeBrowser ? 12 : -1)));
if (ieBrowser && browserVersion < 10) {
console.log("Blob not supported on IE version less than 10");
return;
}
var fileBlob = new Blob([content], {
type: 'text/plain'
});
if (browserVersion > -1) {
window.navigator.msSaveBlob(fileBlob, fileName);
} else {
var downloadLink = document.createElement("a");
downloadLink.download = fileName;
downloadLink.href = window.URL.createObjectURL(fileBlob);
downloadLink.onclick = function (event) { document.body.removeChild(event.target); };
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
downloadLink.click();
}
}