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!

6
  • 1
    Do you mean you want to save it on the user's desktop or on the server? Commented Jun 20, 2013 at 5:08
  • I think you'd have to pass this to a serverside script Commented Jun 20, 2013 at 5: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. Commented Jun 20, 2013 at 5:09
  • @Brett Zamir yeah I want to save this on the server. Commented Jun 20, 2013 at 5:13
  • what server side language are you using ? Commented Jun 20, 2013 at 5:16

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
0

Does this help you?

Saving a text file on server using JavaScript

Also, you can go through this one.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.