Thursday, June 6, 2024
 Popular · Latest · Hot · Upcoming
24
rated 0 times [  28] [ 4]  / answers: 1 / hits: 16869  / 8 Years ago, fri, march 18, 2016, 12:00:00

I want to create text file (notepad type file) using javascript. My code is given below but it is not working plz. Suggest me any solution for creating text file.



var txt = new ActiveXObject(Scripting.FileSystemObject);
var s = txt.CreateTextFile(D:\test.txt, true);// means file is store in my D drive.
s.WriteLine('Hello');
s.Close(); `

More From » jquery

 Answers
16

you can try this:



(function () {
var textFile = null,
makeTextFile = function (text) {
var data = new Blob([text], {type: 'text/plain'});

// If we are replacing a previously generated file we need to
// manually revoke the object URL to avoid memory leaks.
if (textFile !== null) {
window.URL.revokeObjectURL(textFile);
}

textFile = window.URL.createObjectURL(data);

return textFile;
};


var create = document.getElementById('create'),
textbox = document.getElementById('textbox');

create.addEventListener('click', function () {
var link = document.getElementById('downloadlink');
link.href = makeTextFile(textbox.value);
link.style.display = 'block';
}, false);
})();

<textarea id=textbox>Type something here</textarea> <button id=create>Create file</button> <a download=info.txt id=downloadlink style=display: none>Download</a>


Also you can show this DEMO


[#62892] Wednesday, March 16, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
seth

Total Points: 307
Total Questions: 114
Total Answers: 96

Location: Kenya
Member since Mon, Jun 14, 2021
3 Years ago
seth questions
Tue, Feb 8, 22, 00:00, 2 Years ago
Thu, Dec 24, 20, 00:00, 4 Years ago
Sun, Apr 19, 20, 00:00, 4 Years ago
Thu, Apr 9, 20, 00:00, 4 Years ago
;