html5下载属性的任何后备客户端解决方案? [英] Any fallback client-side solutions for the html5 download attribute?

查看:77
本文介绍了html5下载属性的任何后备客户端解决方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于不支持HTML5下载属性的浏览器,是否有客户端回退选项?

Is there a client-side fallback option for browsers that don't support the HTML5 "download" attribute?

目前,只有在Chrome中才能正确支持。 Firefox有支持,但已采取一种钝的观点,它应该只处理来自同一个域的安全问题的文件。

Currently, this is only properly supported in Chrome. Firefox has support, but has taken an obtuse point of view that it should only work on files from the same domain for "security" issues.

处理这个问题的正确方法是有一个后端服务器代理请求的文件与Content-Disposition标头,但在这种情况下,它很可能不是一个选项。

The proper way to handle this is to have a backend server that proxies requested files with a Content-Disposition header, but in this case its most likely not an option.

Firefox的安全剧院isn'非常有用,因为它是一个设置代理的任意机制。

Firefox's "security theater" isn't very helpful either since it is an arbitrary mechanism to setup a proxy.

我在看 https://github.com/dcneiner/Downloadify 但只是意识到它只支持文件创建,而不支持远程文件访问。

I was looking at https://github.com/dcneiner/Downloadify but just realized it only supports file creation, not remote file access.

推荐答案

简短的回答:没有。除了您已经提到的下载属性之外,没有干净的客户端方法可以执行此操作。发送正确的标题是最好的,但是,你可能不想使用它:

The short answer: no. Besides the download attribute that you already mentioned, there is no clean client-side method to do this. Sending the proper header would be best, but there is a hack that you probably don't want to use:

下载属性(您可以使用 document.querySelectorAll('a [download]')),使用 XMLHttpRequest 在HREF中提到的URL处获取页面/数据。然后,使用 btoa() 函数(或用于IE的polyfill)将其转换为base64字符串。现在将data:application / octet-stream; base64,添加到字符串的开头并将其设置为锚点的新HREF属性,然后删除下载属性。 (您可能首先需要探索浏览器支持,诸如 Modernizr )。

For all links with the download attribute (you can get those with document.querySelectorAll('a[download]')), use XMLHttpRequest to get the page/data at the URL mentioned in the HREF. Then, use the btoa() function (or a polyfill for IEs) to convert it to a base64 string. Now add "data:application/octet-stream;base64," to the beginning of the string and set this as the anchor's new HREF attribute, then remove the download attribute. (You probably want to probe browser support first, with something like Modernizr).

我告诉过你,你不喜欢它!

I told you that you wouldn't like it!

这篇关于html5下载属性的任何后备客户端解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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