簡體   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