【发布时间】:2021-07-07 11:34:59
【问题描述】:
我正在尝试通过 JavaScript / jQuery 下载 pdf 文件。我的代码是:
function ShouldGeneratePdf() {
$.ajax({
url: "/Report/EmployeeReport",
type: 'POST',
data: {
EmployeeId: $('#EmployeeId').val()
},
cache: false,
async: false,
success: function (data) {
debugger;
//Convert the Byte Data to BLOB object.
var blob = new Blob([data], { type: "application/pdf" });
//Check the Browser type and download the File.
var isIE = false || !!document.documentMode;
if (isIE) {
window.navigator.msSaveBlob(blob, fileName);
} else {
var url = window.URL || window.webkitURL;
link = url.createObjectURL(blob);
var a = $("<a />");
a.attr("download", "testFile.pdf");
a.attr("href", link);
$("body").append(a);
a[0].click();
$("body").remove(a);
}
},
error: function () {
debugger;
}
});
}
上述方法确实下载了pdf,但下载的文件为空。 知道我在做什么错吗?任何帮助将不胜感激。
PS:PDF 文件也包含图片。
【问题讨论】:
标签: javascript jquery blob