How can I create a .txt file using Jquery?
Using only jQuery/javascript in the browser, and a typically configured web server, this isn't possible. The filesystem of the server is not exposed in such a way that it is directly writeable from a client, that would be a rather large security risk.
To make it possible, you will need to employ some server side code, such as PHP to assist in writing the file on the server. At that point you can send the desired content, and name of the file as a request to the server side code and that code can then write it to your desired location on the server's file system.
Make sure to employ adequate protections so only certain (safe) files can be written to, by the certain users you specify, otherwise you could open the previously mentioned security hole.
diego
Updated on June 20, 2020Comments
-
diego almost 4 years
Ok, this is my domain: example.com/index.html and I want to create a .txt file into the domain. result: example.com/file.txt with this info:
.js:
$('.saveButton').on('click', function (e) { e.preventDefault(); $('.HTMLcontentTosave').html(); //save this html content into a file.txt });
$.ajax maybe?
Thanks a lot!
-
jakebird451 about 10 yearsYou can implement a download on the client size. You can set a link to download from a url:
data:text/plain;charset=UTF-8,data goes here
. This download does not require any interaction between a server.