0

我正在编写一个在 iPad 上运行的 Web 应用程序,但我发现 Chrome iOS 存在下载问题。

您可以通过添加 Chrome iOS 特定进程(如代码)来下载它,但文件名始终为document. 这个问题似乎是由于 Chrome iOS 不支持锚标签的下载属性,有解决方法吗?

const saveCsvFile = (response: AxiosResponse<any>) => {
  const tmp = response.headers['content-disposition'].replace((/attachment; filename\*=UTF-8''(.*)/u), '$1')
  const fileName = decodeURIComponent(tmp)
  if (navigator.userAgent.match('CriOS')) { // iOS Chrome
    const blob = new Blob([response.data])
    const reader = new FileReader()
    reader.onloadend = () => {
      const a = document.createElement('a')
      document.body.appendChild(a)
      a.download = fileName
      a.href = reader.result as string
      a.click()
      a.remove()
    }
    reader.readAsDataURL(blob)
  } else { // Other browser
    const fileURL = window.URL.createObjectURL(new Blob([response.data]))
    const a = document.createElement('a')
    document.body.appendChild(a)
    a.download = fileName
    a.href = fileURL
    a.click()
    a.remove()
    window.setTimeout(() => {
      window.URL.revokeObjectURL(fileURL)
    }, 1000)
  }
}
4

0 回答 0