0% found this document useful (0 votes)
4 views

html - How to create .txt file using JavaScript _ HTML5_ - Stack Overflow

Uploaded by

ABDELLAH ABDICHE
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

html - How to create .txt file using JavaScript _ HTML5_ - Stack Overflow

Uploaded by

ABDELLAH ABDICHE
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

25/10/2024 13:15 html - How to create .txt file using JavaScript / HTML5?

- Stack Overflow

How to create .txt file using JavaScript / HTML5?


Asked 9 years, 4 months ago Modified 7 years, 3 months ago Viewed 57k times

I am new to javascript . all codes available on the internet related to create text file using
javascript is not working in my laptop. can anybody give me idea or with possible code.
12
javascript html

Share Improve this question edited Jun 26, 2015 at 7:46 asked Jun 25, 2015 at 10:48
Follow Nikolay Kostov abc
16.9k 23 88 129 310 1 5 16

possible duplicate of javascript code to save a txt file – Trevi Awater Jun 25, 2015 at 10:50

2 You should define "is not working". Maybe your browser is too old? Some of the examples use fairly
new techniques. – Sirko Jun 25, 2015 at 10:50

2 Answers Sorted by: Highest score (default)

This code should work, give this a try and if this doesn't work then it may be an issue with
your browser:
9
(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);
})();

https://stackoverflow.com/questions/31048215/how-to-create-txt-file-using-javascript-html5 1/2
25/10/2024 13:15 html - How to create .txt file using JavaScript / HTML5? - Stack Overflow

And the HTML:

<textarea id="textbox">Type something here</textarea> <button id="create">Create


file</button>
<a download="info.txt" id="downloadlink" style="display: none">Download</a>

Taken from this Fiddle:

http://jsfiddle.net/uselesscode/qm5ag/

Share Improve this answer Follow edited Jun 14, 2017 at 11:08 answered Jun 25, 2015 at 10:54
n00dl3 Web Develop Wolf
21.5k 8 67 77 6,286 13 59 114

its not working in both IE as well as in Chrome..what should i do? – abc Jun 25, 2015 at 11:18

It works in Firefox 38 and Chrome 43, but not IE10/11. The text file is generated and can be saved by
right click > save as, but the listener for the download link doesn't work. I can't find an example that
works in IE. – Equalsk Jun 26, 2015 at 8:44

A very fast and easy solution is to use FileSaver.js :


https://raw.githubusercontent.com/eligrey/FileSaver.js/master/FileSaver.js
3
Then it takes only 2 lines of code to download a txt file :

var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});

saveAs(blob, "hello world.txt");

This code example will display a dialog box to download a file named "hello world.txt"
containing the text "Hello, world!". Just replace this by the file name and the text content of
your choice !

Share Improve this answer Follow answered Jan 16, 2016 at 12:45
Ismael EL ATIFI
2,088 22 16

Highly active question. Earn 10 reputation (not counting the association bonus) in order to answer this
question. The reputation requirement helps protect this question from spam and non-answer activity.

https://stackoverflow.com/questions/31048215/how-to-create-txt-file-using-javascript-html5 2/2

You might also like