我有一个在线配置编辑器放在这里上。配置编辑器是一个单页文件,即通过链接托管的index.html文件。
我希望能够以一种在单击链接时触发下载index.html文件的方式链接到这个配置编辑器,而不是在浏览器中显示它。
我本来希望通过下面的方法来实现这一点--但它似乎不适用于index.html文件。有什么建议吗?如何通过HTML或简单的Javascript代码解决这个问题?
<a href="https://[url]/index.html" download="editor">download here</a>发布于 2020-02-27 21:10:18
我最终得到了这个解决方案--任何改进它的投入都是受欢迎的。请注意,除非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>https://stackoverflow.com/questions/60437238
复制相似问题