我JQuery的阿賈克斯POST請求的樣子:
function LoadPDF() {
var args = [];
var x = $.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: OnSuccess,
error: OnErrorCall
});
function OnSuccess(response) {
var byteArray = new Uint8Array(response.d);
saveTextAsFile("document.pdf", byteArray);
}
function OnErrorCall(response) {
console.log(response);
//location.reload();
}
}
嘗試文件 - 保存 - as.js,像這樣:
function saveTextAsFile(fileNameToSaveAs, textToWrite) {
/* Saves a text string as a blob file*/
var ie = navigator.userAgent.match(/MSIE\s([\d.]+)/),
ie11 = navigator.userAgent.match(/Trident\/7.0/) && navigator.userAgent.match(/rv:11/),
ieEDGE = navigator.userAgent.match(/Edge/g),
ieVer = (ie ? ie[1] : (ie11 ? 11 : (ieEDGE ? 12 : -1)));
if (ie && ieVer < 10) {
console.log("No blobs on IE ver<10");
return;
}
var textFileAsBlob = new Blob([textToWrite], {
type: 'text/plain'
});
if (ieVer > -1) {
window.navigator.msSaveBlob(textFileAsBlob, fileNameToSaveAs);
} else {
var downloadLink = document.createElement("a");
downloadLink.download = fileNameToSaveAs;
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
downloadLink.onclick = function (e) { document.body.removeChild(e.target); };
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
downloadLink.click();
}
}
對不起,它似乎並沒有工作。當我嘗試將responseType設置爲您在示例中指定的內容時,我也會收到此錯誤消息:'提供的值'text/plain; charset = UTF-8'不是一個有效的枚舉值類型XMLHttpRequestResponseType.' –
老實說,我不知道爲什麼text/plain;對你來說是無效的價值;也許嘗試刪除「charset = UTF-8」,並只使用「text/plain」,希望UFT-8是默認的? – user1730026
將我的答案修改爲包含file-save-as.js實現。希望能幫助到你 – user1730026