handleDownload() 函数作为事件处理程序(onclick) 添加到按钮,以便用户可以下载文件。用户可以下载但文件已损坏。我们如何防止文件损坏?
function handleDownload(){
const domain = window.location.origin;
const url =`${domain}/images/athar.pdf`
fetch(url).
then(response=>response.blob()).
then(blob=>{
const blobURL= window.URL.createObjectURL(
new Blob([blob]))
const filename = 'athar.pdf'
const aTag = document.createElement('a')
aTag.href=blobURL
aTag.setAttribute('download',filename)
document.body.appendChild(aTag)
aTag.click()
aTag.remove()
}).
catch(e=>console.log(e))
} Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
由于您已经收到 Blob 形式的响应,因此无需再次将其转换为 Blob,因此请尝试删除该部分。
尝试替换:
const blobURL= window.URL.createObjectURL( new Blob([blob]))这样: