首页 > 解决方案 > 无法在 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();
},

它适用于所有提到的浏览器。为什么?

标签: javascripthtmlbrowsercross-browserdownload

解决方案


您可以使用 HTML5download属性。

<a href="/path/to/file" download>

您可以使用 javascript 相应地更改 href 值。


推荐阅读