我正在尝试支持从 iOS Edge 中的 API 下载文件。该文件作为 blob 下载,我们已经有了在 Safari 和 Chrome 上下载的解决方案(分别使用 createObjectUrl
和 readAsDataUrl
),但这些似乎都不起作用在 iOS 的 Edge 上。
我在网上找不到任何有关如何执行此操作的信息,甚至 FileReader 似乎也不支持它。
我尝试使用在 iOS 上的 Chrome 和 iOS 上的 Safari 中一致工作的解决方案进行下载。
编辑:iOS 上的 Edge 使用 Safari 的渲染引擎,因此任何 IE11 或 Edge on Desktop 专注的解决方案在这里都不起作用。
最佳答案
在IE/Edge浏览器中,您可以尝试使用window.navigator.msSaveOrOpenBlob方法下载文件。您可以查看这些文章:thread 1和 article 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/