javascript - 无法在 Firefox 中以编程方式下载 html5 下载(适用于 Safari、IE、Opera、Chrome)
问题描述
onDownload: function() {
var element = document.createElement("a");
element.setAttribute("href", "/metadata/rest/swagger?namespace=" + this.namespace);
element.click();
},
为按钮添加下载功能。但它在 Firefox 中不起作用。(适用于 Safari、IE、Opera、Chromes)
通过以下方式更改代码后:
在 html 页面中创建锚 (<a>),使其隐藏并在之前给出一个 id。通过 id 获取元素。设置href属性
onDownload: function() {
var element = document.getElementById("download");
element.setAttribute("href", "/metadata/rest/swagger?namespace=" + this.namespace);
element.click();
},
它适用于所有提到的浏览器。为什么?
解决方案
您可以使用 HTML5download
属性。
<a href="/path/to/file" download>
- 有关 HTML5
download
属性的更多信息,您可以阅读Anchor 元素 - MDN Docs - 对于浏览器支持检查here
您可以使用 javascript 相应地更改 href 值。
推荐阅读
- javascript - 检查 JSON 值是否已定义的更好方法?
- java - 确定数组是否包含重复项
- asp.net - 如何将接口绑定到 Razor 表单
- csv - 使用 Jmeter 测试具有相同列标题的多个 csv 的 1 亿条记录的 API 的合适方法
- reactjs - RSuite Cascader 搜索文本字段不起作用
- c# - 调整窗口大小时,WPF 使拉伸的 TextBlock 收缩
- mysql - 如何在SQL中选择列的增量总和
- windows - 如何在 Windows 上使用 LuaRocks 安装 lua-zlib?
- java - 试图在我的java代码中打印数组
- php - 如何在 PHP 中打印对象数组的一个元素?