4

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!

Surreal Dreams
  • 26,055
  • 3
  • 46
  • 61
diego
  • 77
  • 1
  • 3
  • 12
  • 1
    Do you mean you want to save it on the user's desktop or on the server? – Brett Zamir Jun 20 '13 at 05:08
  • I think you'd have to pass this to a serverside script – Michael L Watson Jun 20 '13 at 05:09
  • If you mean a server, you can use a PUT Ajax request...If the user's desktop, I don't believe jQuery will help you, but certain browsers allow methods of saving a local file. – Brett Zamir Jun 20 '13 at 05:09
  • @Brett Zamir yeah I want to save this on the server. – diego Jun 20 '13 at 05:13
  • what server side language are you using ? – Sakthivel Jun 20 '13 at 05:16
  • @user2503703 JQuery is a javascript library which runs on the client side/browser. To write to the server you need to use a server side tool. example. Python, Php etc. If you want to create file in server using ajax request. You my want to have a look here [Create an XML file in server using SimpleXML and JQuery Aja](http://stackoverflow.com/questions/11001127/create-an-xml-file-in-server-using-simplexml-and-jquery-ajax) – captaindroid Jun 20 '13 at 05:49

3 Answers3

3

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.

Chris Cherry
  • 28,118
  • 6
  • 68
  • 71
  • You 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. – jakebird451 Feb 26 '14 at 19:41
0

Does this help you?

Saving a text file on server using JavaScript

Also, you can go through this one.

Community
  • 1
  • 1
Vandesh
  • 6,368
  • 1
  • 26
  • 38
0

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.

Brett Zamir
  • 14,034
  • 6
  • 54
  • 77