前端开发那些事儿

js 解决下载pdf文件空白

2021-07-13  本文已影响0人  small_zeo
const downLoadFunc = (imgSrc, imgName) => {
    if (!imgSrc) return
    const fileExt = imgSrc.split('.').pop().toLocaleLowerCase()
    if (fileExt == 'pdf') {
        fetch(imgSrc).then(function (response) {
            response.arrayBuffer().then(res => {
                let type = 'application/pdf;charset-UTF-8' // 资源类型
                let blob = new Blob([res], {type: type})
                let objectUrl = URL.createObjectURL(blob)
                let link = document.createElement('a')
                link.style.display = 'none'
                link.href = objectUrl
                link.setAttribute('download', imgName)
                document.body.appendChild(link)
                link.click()
                document.body.removeChild(link)
            })
        })
    } 
}
上一篇下一篇

猜你喜欢

热点阅读