繁体   English   中英

是否可以使用 html 上的输入值保存文本并使用 JavaScript 写入本地 txt 文件?

[英]Is it possible to save texts from input value on html and write on local txt file by using JavaScript?

是否可以使用 html 上的输入值保存文本并使用 JavaScript 写入本地 txt 文件?

另外我想知道是否有最好的方法通过使用其他后端语言来实现我想要实现的目标。

此处提供工作示例的完整说明: https://robkendal.co.uk/blog/2020-04-17-saving-text-to-client-side-file-using-vanilla-js

HTML

<fieldset>
  <legend>Enter some config details</legend>
  <textarea></textarea>
  <button id="btnSave">save config</button>
</fieldset>

JS

const downloadToFile = (content, filename, contentType) => {
      const a = document.createElement('a');
      const file = new Blob([content], {type: contentType});
      
      a.href= URL.createObjectURL(file);
      a.download = filename;
      a.click();
    
        URL.revokeObjectURL(a.href);
    };
    
    document.querySelector('#btnSave').addEventListener('click', () => {
      const textArea = document.querySelector('textarea');
      
      downloadToFile(textArea.value, 'my-new-file.txt', 'text/plain');
    });

暂无
暂无

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

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