Reputation: 5508
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? ).
Upvotes: 2
Views: 581
Reputation: 70523
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.
Upvotes: 4
Reputation: 3126
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 ); } });
Upvotes: 1
Reputation: 8268
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.
Upvotes: 0
Reputation: 138051
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.
Upvotes: 0
Reputation: 12323
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.
Upvotes: 1
Reputation: 32082
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.
Upvotes: 1
Reputation: 34168
It can if the "file" is a cookie. I will leave research on that up to the reader.
Upvotes: 2
Reputation: 67832
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.
Upvotes: 1