如何通过 HTML 中的 <a> 标签强制下载 "index.html" 文件

How to force download of "index.html" file via an <a> tag in HTML

我放置了一个在线配置编辑器 here。配置编辑器是一个单页文件,即 index.html 文件,通过 link.

托管

我希望能够 link 通过单击 link 触发下载 index.html 文件的方式访问此配置编辑器 - 而不是在中显示它浏览器。

我希望通过下面的方式实现这一点 - 但它似乎不适用于 index.html 文件。关于如何通过 HTML 或简单的 Javascript 代码解决此问题的任何建议?

<a href="https://[url]/index.html" download="editor">download here</a>

我最终得到了这个解决方案 - 欢迎任何改进它的意见。请注意,除非 index.html 在同一域中,否则此解决方案会导致 CORS 问题,但由于我恰好是这种情况,因此它可以按需工作。

HTML:

<a href="#" id="download-editor" class="btn-white">Offline editor</a>

Javascript:

<script>
  window.onload = function() {
    var a = document.getElementById("download-editor");
    a.onclick = function() {

    var url = 'https://canlogger.csselectronics.com/simple-editor/index.html'

    fetch(url).then(function(t) {
      return t.blob().then((b)=>{
        var a = document.createElement("a");
        a.href = URL.createObjectURL(b);
        a.setAttribute("download", 'config-editor.html');
        a.click();
      }
      );
    });
    }
  }
</script>