Can javascript be used to write to a file ? Sorry restate that (Can javascript be used to write to a file on the web server it is hosted on? ).
-
4Maybe we shouldn't help someone called "H4cKL0rD" on how to use JavaScript to write files on a server that presumably they don't have access to? Other hacking questions he's asked eg http://stackoverflow.com/questions/775481/keeping-address-in-c-hacking-game-code http://stackoverflow.com/questions/574159/what-is-a-buffer-overflow-and-how-do-i-cause-one – Pool Jan 21 '10 at 21:52
9 Answers
They used to call cookies "really small files on your computer", so I say: "Yes, yes it can."
update based on edit
Yes, you have to use AJAX to call a web service on the web server.

- 69,564
- 10
- 76
- 117
-
Creative (and true), but probably not what the op wanted. +1 for creativity and a literal answer. – Stefan Kendall Jan 21 '10 at 21:38
It can if the "file" is a cookie. I will leave research on that up to the reader.

- 32,614
- 12
- 69
- 100
Not cross-compatibly, unless you do certain trickery. A trusted applet, for example, has complete access to the user's file system, and it can expose javascript methods. Expose a write method in a trusted applet, and you're good to go.

- 66,414
- 68
- 253
- 406
-
I may or may not have abused this method in the past. It's scary how much control an applet can get. – Stefan Kendall Jan 21 '10 at 22:50
No. if you need to store data on the client, you can use cookies, Google Gears, or the client-side storage supported by modern browsers like firefox and safari.
The YUI Storage Utility is a nice cross-browser wrapper around these methods.

- 31,211
- 14
- 77
- 99
You could make an Ajax call to a web service which would either save the file for you on the server or perhaps serve it back to the browser for download
using jQuery
$.ajax({ type: "POST", url: "save.php", data: "name=Bob&Age=1", success: function(msg){ alert( "Data Saved: " + msg ); } });

- 3,106
- 2
- 32
- 57
-
1+1 for trying to work the problem (with a lack of information about what is exactly required) rather than just give a simple "no" (or "yes, if it's a cookie"). – Beska Jan 21 '10 at 21:43
On it's own, no.. at least without exploiting browser / plugin vulnerabilities.
Since you clarified your question:
It can be used to do an ajax call to a server-side script which would then write a file into the file system.

- 12,025
- 4
- 35
- 53
Javascript cannot directly write to a file on the webserver on which it is hosted. If it is required, you can use something like an XMLHttpRequest object to communicate your means to a serverside script (like PHP or Python), which will then do the write.
Javascript code is executed on the client side. It does not directly have access to your server.

- 134,922
- 42
- 253
- 328
It depends on a platform. Some platforms (like Windows), allow javascript to access filesystem (in Windows - via FileSystemObject ActiveX object). But client's browser security settings must be adjusted to allow this.

- 8,023
- 3
- 32
- 40