首页 > 解决方案 > 是否可以在 iOS 上的 Edge 中下载 blob 文件?

问题描述

我正在尝试支持从 iOS 的 Edge 中的 API 下载文件。该文件作为 blob 下载,我们已经为在 Safari 和 Chrome 上下载(分别使用createObjectUrlreadAsDataUrl)提供了解决方案,但这些似乎都不适用于 iOS 上的 Edge。

我在网上找不到有关如何执行此操作的任何信息,甚至 FileReader 似乎也不支持它。

我尝试使用在 iOS 上的 Chrome 和 iOS 上的 Safari 中始终有效的解决方案进行下载。

编辑:iOS 上的 Edge 使用 Safari 的渲染引擎,因此任何 IE11 或 Edge on Desktop 的解决方案都不会在这里工作。

标签: javascriptiosblobmicrosoft-edge

解决方案


在 IE/Edge 浏览器中,您可以尝试使用 window.navigator.msSaveOrOpenBlob 方法下载文件。您可以查看这些文章:线程 1文章 1。像这样的代码:

showFile(blob){
  // It is necessary to create a new blob object with mime-type explicitly set
  // otherwise only Chrome works like it should
  var newBlob = new Blob([blob], {type: "application/pdf"})

  // IE doesn't allow using a blob object directly as link href
  // instead it is necessary to use msSaveOrOpenBlob
  if (window.navigator && window.navigator.msSaveOrOpenBlob) {
    window.navigator.msSaveOrOpenBlob(newBlob);
    return;
  } 

  // For other browsers: 
  // Create a link pointing to the ObjectURL containing the blob.
  const data = window.URL.createObjectURL(newBlob);
  var link = document.createElement('a');
  link.href = data;
  link.download="file.pdf";
  link.click();
  setTimeout(function(){
    // For Firefox it is necessary to delay revoking the ObjectURL
    window.URL.revokeObjectURL(data);
  , 100}
}

推荐阅读