javascript - 是否可以在 iOS 的 Edge 中下载 blob 文件?

标签 javascript ios blob microsoft-edge

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

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

我尝试使用在 iOS 上的 Chrome 和 iOS 上的 Safari 中一致工作的解决方案进行下载。

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

最佳答案

在IE/Edge浏览器中,您可以尝试使用window.navigator.msSaveOrOpenBlob方法下载文件。您可以查看这些文章:thread 1article 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}
}

关于javascript - 是否可以在 iOS 的 Edge 中下载 blob 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54358179/

相关文章:

excel - 如何在React JS中将blob数据转换为可下载的excel/CSV文件

javascript - 尝试使用连接器表连接 Sequelize 中的两个数据库表

javascript - 获取复选框的选中值并将其发送到 servlet

ios - 在 Swift 中实例化带有标识符的 View Controller

ios - ld : 1 duplicate symbol for architecture arm64, swift3 中的两个 SDK

javascript - 如何在 .run 函数中传递 blob

javascript - SVG旋转和缩放: How to change the rotation/scaling center?

javascript - 如何将 Leaflet 坐标切换到 0,0 西南方向?

ios - dateByAddingComponents :toDate:options return nil? 什么时候

mysql - Mysql或sqlite Blob数据类型可以在其中存储varchar数据吗?