I'm trying to make the browser download a pdf file received from an ajax response.
Inspired by Download pdf file using jquery ajax I simulate a click/download event like this:
var req = new XMLHttpRequest();
req.open("POST", "/servicepath/Method?ids=" + ids, true);
req.responseType = "blob";
req.onreadystatechange = function () {
if (req.readyState === 4 && req.status === 200) {
var blob = req.response;
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = "PdfName-" + new Date().getTime() + ".pdf";
link.click();
}
};
req.send();
Unfortunately this only works in Chrome, but not Firefox + IE. Nothing happens when I try to trigger it in the last two browsers.
The script and markup is placed inside an iframe due to inheritance from an CMS, but I'm not sure if that has any influence a.
Any idea on how to optimize it for all modern browsers?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…