img

微信内置浏览器下载pdf的时候标题为null?

2023-05-19 0条评论 887次阅读 JavaScript


0.jpg

我的解决方法是:

function download (url, fileName) {
  const x = new XMLHttpRequest()
  x.responseType = 'blob'
  x.open('GET', url, true)
  x.send()
  x.onload = () => {
    const downloadElement = document.createElement('a')
    const href = window.URL.createObjectURL(x.response) // create download url
    downloadElement.href = href
    downloadElement.download = fileName // set filename (include suffix)
    document.body.appendChild(downloadElement) // append <a>
    downloadElement.click() // click download
    document.body.removeChild(downloadElement) // remove <a>
    window.URL.revokeObjectURL(href) // revoke blob
  }
}
export { download }

原文地址在这里:点击这里,我的 GitHub工具箱 记录很多这样处理方法的笔记,有兴趣的铁铁可以看一下

💬 COMMENT


🦄 支持markdown语法

👋友