简体   繁体   English

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

[英]Is it possible to download a blob file in Edge on iOS?

I'm trying to support downloading a file from an API in Edge on iOS.我正在尝试支持从 iOS 版 Edge 中的 API 下载文件。 The file is downloaded as a blob, and we already have solutions in place for downloading on Safari and Chrome (Using createObjectUrl and readAsDataUrl respectively), but neither of these seem to work on Edge on iOS.该文件作为 blob 下载,我们已经有了在 Safari 和 Chrome 上下载的解决方案(分别使用createObjectUrlreadAsDataUrl ),但这些似乎都不适用于 iOS 上的 Edge。

I can't find anything online around how to do this, and even FileReader doesn't seem to support it.我在网上找不到任何关于如何执行此操作的信息,甚至 FileReader 似乎也不支持它。

I have tried to download using solutions that work consistently in Chrome on iOS and Safari on iOS.我尝试使用在 iOS 上的 Chrome 和 iOS 上的 Safari 中一致工作的解决方案进行下载。

Edit: Edge on iOS uses Safari's rendering engine, so any IE11 or Edge on Desktop focused solutions will not work here.编辑:iOS 上的 Edge 使用 Safari 的渲染引擎,因此任何 IE11 或 Edge on Desktop 解决方案在这里都不起作用。

In the IE/Edge browser, you could try to use window.navigator.msSaveOrOpenBlob method to download the file.在 IE/Edge 浏览器中,您可以尝试使用 window.navigator.msSaveOrOpenBlob 方法下载文件。 You could check these article: thread 1 and article 1 .您可以查看这些文章: thread 1article 1 Code like this:像这样的代码:

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}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM