无法在Safari(iPhone / iPad)上下载文件

时间:2019-05-22 09:03:48

标签: javascript pdf download mobile-safari

我有一个功能可以帮助我下载项目的PDF文件,并且可以跨浏览器运行(iOS上的Safari除外)

export function downloadFile(
  fileData,
  fileName = 'file',
  fileExt = 'pdf',
  fileType = 'application/pdf',
) {
  const downloadLink = document.createElement('a');
  downloadLink.target = '_blank';
  downloadLink.download = `${fileName}.${fileExt}`;
  const blob = new Blob([fileData], {
    type: fileType,
  });
  const URL = window.URL || window.webkitURL;
  if (window.navigator && window.navigator.msSaveOrOpenBlob) {
    window.navigator.msSaveOrOpenBlob(blob, `${fileName}.${fileExt}`);
  } else {
    const downloadUrl = URL.createObjectURL(blob);
    window.open(downloadUrl, '_blank');
    downloadLink.href = downloadUrl;
    document.body.appendChild(downloadLink);
    downloadLink.click();
    document.body.removeChild(downloadLink);
    URL.revokeObjectURL(downloadUrl);
  }
}

它失败并在页面上产生此错误( WebKitBlobResource错误1

0 个答案:

没有答案