javascript - 是否可以在 iOS 上的 Edge 中下载 blob 文件?
问题描述
我正在尝试支持从 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 方法下载文件。您可以查看这些文章:线程 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}
}
推荐阅读
- c# - 正则表达式 - 匹配与组
- react-native - 从 stack-navigator-options 使用 props 调用 redux 操作
- scala - scala 测试:如何混合并行和顺序执行 FeatureSpec 测试套件
- php - cakephp 按不同分组
- android - 我的应用程序崩溃,无一例外“HandleSigQuit DumpForSigQuit”
- docusignapi - DocuSign invalid_grant 错误
- python - 熊猫不尊重 matplotlib 轴?
- tensorflow - 如何在 ubuntu 上训练 tensorflow 模型?
- python - Python 2D 数组和循环
- typo3 - TYPO3 的学习扩展开发资源?