Reputation: 115
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????
Upvotes: 2
Views: 1409
Reputation: 778
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
Upvotes: 1
Reputation: 2383
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');
Upvotes: 2