我从后端发送字节数组,并试图用ajax和JS打开它,我总是有损坏的PDf无法打开。我的代码如下。
$.ajax({
responseType: 'application\pdf',
sucess: function (response)
{
var blob=new blob([response]),{type:'application\pdf'};
window.navigator.msSaveOrOpen(blob);
}
});
任何帮助都将不胜感激。谢谢
发布于 2019-04-23 10:22:55
首先,在成功函数中设置一个断点,然后尝试使用F12开发人员工具来调试您的代码,并确保您可以获得pdf blob。然后,使用window.navigator.msSaveOrOpenBlob()方法下载pdf文件。
代码如下:
var req = new XMLHttpRequest();
req.open("GET", "/44678.pdf", true);
req.responseType = "blob";
req.onload = function (event) {
var blob = req.response;
var newBlob = new Blob([blob], { type: "application/pdf" })
// IE doesn't allow using a blob object directly as link href
// instead it is necessary to use msSaveOrOpenBlob
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(newBlob);
return;
}
};
更多细节,你可以查看this article。
编辑:请检查您的代码,Ajax方法没有请求url,并且在成功函数处有拼写错误。
https://stackoverflow.com/questions/55799733
复制相似问题