【发布时间】:2021-12-07 05:38:04
【问题描述】:
以下代码可以正常下载远程图像,但在更改它以下载远程 PDF 文件时不起作用,这是我正在使用的代码:
<a href="https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf" data-remote>Remote PDF</a>
<script defer type="text/javascript">
const a = document.querySelector('a[data-remote]')
a.addEventListener('click', async (e) => {
e.preventDefault()
const file = await fetch(e.target.href);
const blob = await file.blob();
const blobUrl = URL.createObjectURL(blob);
const downloadLink = document.createElement("a");
downloadLink.href = blobUrl;
downloadLink.download = 'file.pdf';
downloadLink.click();
})
</script>
【问题讨论】:
标签: javascript html pdf download