简体   繁体   English

如何使用 javascript 将一些文本内容下载为 .ftl 文件?

[英]How to download some text content as a .ftl file using javascript?

I have some text content, getting from one api call.我有一些文本内容,来自一个 api 调用。 On a button click I want to download that text content as a .ftl file.单击按钮时,我想将该文本内容下载为 .ftl 文件。 I have used "data:text/plain;charset=utf-8," , but after using this I am getting downloaded file as .txt.我已经使用了"data:text/plain;charset=utf-8," ,但是在使用它之后,我下载的文件为 .txt。 I want to download as a .ftl file.我想下载为 .ftl 文件。

You could define a form with a function call:您可以使用函数调用定义表单:

<form onsubmit="download('file.ftl', [your.data])">
  <input type="submit" value="Download">
</form>

The download function:下载功能:

function download(filename, text) {
  var element = document.createElement('a');
  element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
  element.setAttribute('download', filename);
  
  element.style.display = 'none';
  document.body.appendChild(element);

  element.click();

  document.body.removeChild(element);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM