2

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? ).

alemjerus
  • 8,023
  • 3
  • 32
  • 40
H4cKL0rD
  • 5,421
  • 15
  • 53
  • 74
  • 4
    Maybe 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 Answers9

4

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.

Hogan
  • 69,564
  • 10
  • 76
  • 117
2

It can if the "file" is a cookie. I will leave research on that up to the reader.

Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
1

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.

Stefan Kendall
  • 66,414
  • 68
  • 253
  • 406
1

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.

Gabe Moothart
  • 31,211
  • 14
  • 77
  • 99
1

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 );
   }
 });

acheo
  • 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
1

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.

code_burgar
  • 12,025
  • 4
  • 35
  • 53
0

Unless used in server-side contexts: no, sorry.

ChristopheD
  • 112,638
  • 29
  • 165
  • 179
0

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.

zneak
  • 134,922
  • 42
  • 253
  • 328
0

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.

alemjerus
  • 8,023
  • 3
  • 32
  • 40