![](/img/trans.png)
[英]How to download a blob file from Chrome iOS in Javascript?
[英]Is it possible to download a blob file in Edge on iOS?
我正在嘗試支持從 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}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.