Reputation: 15
I'm trying to create a comments tab for my website and insert an HTML form.
So far my form and comments tab is ready. I'd like to create a text document to store HTML form inputs.
I couldn't find a proper way to do that, like C++'s fstream
. I found some code with PHP, but I don't want to use PHP either.
Any way to do this?
Upvotes: 1
Views: 24525
Reputation: 13828
You can use datauri and new download
property of anchor elements (<a>
) to achive it, without a server. Just randomly type something in the text box, click "export" and see what happens:
var container = document.querySelector('textarea');
var anchor = document.querySelector('a');
anchor.onclick = function() {
anchor.href = 'data:text/plain;charset=utf-8,' + encodeURIComponent(container.value);
anchor.download = 'export.txt';
};
<textarea></textarea>
<p><a href="#">Export</a></p>
You can achive other types of file download, just change text/plain
to the proper MIME type, and ensure that you encode the file content correctly. For example, <canvas>
is a good approach for generating images.
Upvotes: 11