Я хочу скачать файл PDF, когда нажимаю кнопку HTML. Я хочу, чтобы событие click было в файле JS.
Мне нужно что-то такое же, что и код ниже, но в файле JS:
<a href="url" download>
<button>Download</button>
</a>
Я пытался следовать этомуно это не создает мою кнопку:
var req = new XMLHttpRequest();
req.open("GET", "url", true);
req.responseType = "blob";
req.onreadystatechange = function () {
if (req.readyState === 4 && req.status === 200) {
// test for IE
if (typeof window.navigator.msSaveBlob === 'function') {
window.navigator.msSaveBlob(req.response, "PdfName-" + new Date().getTime() + ".pdf");
} else {
var blob = req.response;
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = "PdfName-" + new Date().getTime() + ".pdf";
// append the link to the document body
document.body.appendChild(link);
link.click();
}
}
};
req.send();