2

I want to append data into a txt file. I looked at other questions, and all of the answers were only supported in IE. This is what I have so far (I'm a complete JavaScript rookie, so I don't know anything about doing this kind of stuff):

var word = "word"; //Missing Code

What is the pure JavaScript code here????

anonsaicoder9
  • 107
  • 1
  • 12
  • Hi, please take a look to this answer https://stackoverflow.com/a/51383975/615274 – Mario Jun 06 '20 at 03:45
  • this answer might help you [answer](https://stackoverflow.com/questions/62191155/how-to-clone-html-page-when-button-cllick/62192400#62192400) – Eissa Saber Jun 06 '20 at 09:22

2 Answers2

2

I found old answer here

const createTextFile = (fileNmae, text) => {
  const element = document.createElement('a');

  element.setAttribute(
    'href',
    'data:text/plain;charset=utf-8,' + encodeURIComponent(text),
  );
  element.setAttribute('download', fileNmae);

  element.style.display = 'none';
  document.body.appendChild(element);

  element.click();

  document.body.removeChild(element);
};

createTextFile('test.txt', 'word');


Ahmed ElMetwally
  • 2,276
  • 3
  • 10
  • 15
1
let data = "some"
let file = new Blob([data], {type: "txt"})

// Appending to the file can be mimiced this way
data = data+"Text"
file = new Blob([data], {type: "txt"})

// To Download this file
let a = document.createElement("a"), url = URL.createObjectURL(file);
a.href = url;
a.download = file;
document.body.appendChild(a);
a.click()
setTimeout(function() {
  document.body.removeChild(a);
}, 0);

Hope this can help

thealpha93
  • 780
  • 4
  • 15