Safari/iOS 中“下载"属性的替代方案 [英] Alternative for 'download' attribute in Safari/iOS

查看:23
本文介绍了Safari/iOS 中“下载"属性的替代方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 base64 创建的 blob,我需要将此数据作为 pdf 格式下载.

I have a blob created with a base64, and I need to make this data downloadable as a pdf.

我创建了这个片段:

    var blob = new Blob([byte]);
    var link = document.createElement('a');

    link.href = window.URL.createObjectURL(blob);
    link.target = '_blank';
    var fileName = name + '.pdf';
    link.download = fileName;
    link.click();

它适用于所有浏览器,除了 iOS 上的 safari mobile.

It works on all the browsers, except than safari mobile on iOS.

文件确实被下载了,但它的名字是未知",然后它无法打开,因为扩展名丢失了.

The file gets actually downloaded, but its name is "unknown", then it can't be open since the extension gets lost.

问题在于下载属性在此浏览器和 IE 上缺乏支持.

The problem is that the download attribute lacks support on this browser and IE.

IE 有很多变通方法,但我没有找到任何适用于 safari/iOS 的方法.

There are a lot of workarounds for IE, but I didn't find any for safari/iOS.

您知道如何在此浏览器中下载从 base64(不涉及 XHR)获取的 blob?

Do you know how can I download a blob got from a base64 (no XHR involved) in this browser?

谢谢

推荐答案

我需要在 Safari iOS 中将此数据以 pdf (...) 格式下载

I need to make this data downloadable as a pdf (...) in safari iOS

简短回答:你不能.由于这个 bug 无法在 Safari iOS 上下载文件

SHORT ANSWER: you can't. Due this bug is impossible to download the file on safari iOS

另一种方法是在浏览器上使用正确的 mime 类型打开文件,以便它可以显示其内容(然后用户可以根据需要手动下载它).

The alternative is to open the file on the browser with the proper mime type, so it can show its content (and the user can then manually download it if needed).

确保在创建 Blob 时传递 mime 类型.参考

Make sure to pass mime type when creating the Blob. reference

var blob = new Blob([byte], {type: 'application/pdf'});

<小时>

最后,我强烈建议您使用可以处理的 FileSaver.js大多数极端情况/多浏览器支持在 javascript 中保存(或在本例中打开)文件.


Lastly, I'd strongly suggest you to use FileSaver.js which that can handle most of the corner cases/multiple browser support for save (or in this case, open) a file in javascript.

这篇关于Safari/iOS 中“下载"属性的替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆