簡體   English   中英

如何通過<a>HTML 中</a>的<a>標簽</a>強制下載“index.html”文件

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

在這里放置 一個在線配置編輯器。 配置編輯器是一個單頁文件,即index.html文件,通過鏈接托管。

我希望能夠以一種在單擊鏈接時觸發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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM