无法在 iphone 或 ipad pro ios12 上的 safari 浏览器中下载 PDF

Kal*_*hir 5 javascript jquery reactjs react-redux ios12

我的网络应用程序中有 PDF 下载功能。它适用于所有浏览器和 iOS11,但不适用于移动或 iod pro 上的 safari 浏览器和 ios12。我收到以下错误 - WebKitBlobResource 错误 1

export const downloadPDF = (downloadLink, fileName, trackId, productId, historyId) => {
  return (dispatch) => {
    return request.doGetAuth(downloadLink).then(response => {
      let contentType = response.headers.get('content-type');
      if (_.includes(contentType, 'application/json')) {
        return response.json();
      } else {
        return response.blob();
      }
    }).then(blobby => {
      if (!blobby.message) {
        const blob = new Blob([blobby], {
          type: 'application/pdf'
        });
        if (isIos()) {
          if (!isCriOs()) {
            // For ios 
            let url = window.URL.createObjectURL(blob);
            dispatch(downloadReadyAction(url, fileName));
          } else {
            // if chrome
            let reader = new FileReader();
            reader.onload = function(e) {
              dispatch(downloadReadyAction(reader.result, fileName));
            }
            reader.readAsDataURL(blob);
          }
        } else {
          FileSaver.saveAs(blob, fileName);
        }
      }
    }).catch(err => {
      console.log('Problem downloading pdf from server ' + err)
    })
  }
}
Run Code Online (Sandbox Code Playgroud)

Kal*_*hir 5

当我们在新的 url 选项卡中打开 pdf 时,该文件不存在,但它的唯一缓存存储在浏览器中。因此,当我们生成 blob 并重定向到当前选项卡以指向生成的 blob url 时,我们丢失了缓存。所以在新窗口中打开 url 有帮助。

   let url = window.URL.createObjectURL(blob);
   window.open(url, "_blank");
Run Code Online (Sandbox Code Playgroud)

  • 我相信,如果您将“contentType”更改为“application/octet-stream”,它将下载而不是嵌入。 (2认同)