Skip to content Skip to sidebar Skip to footer

How Can I Create A .txt File Using Jquery?

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', func

Solution 1:

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.


Solution 2:

Does this help you?

Saving a text file on server using JavaScript

Also, you can go through this one.


Solution 3:

Technically, the "right" way would be to make a PUT request (the "type" argument: see http://api.jquery.com/jQuery.ajax/), but this is apparently not supported on all browsers, and some servers may also block these by default so you'd need to get around this. I say the "right" way because a PUT request is intended to mean that the consequence will be to save a file at the pointed location.

The more common approach would be to allow a POST request but you'd need a server-side script to do things like validate that a user had permission to make the request.


Post a Comment for "How Can I Create A .txt File Using Jquery?"