By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,692 Members | 1,482 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,692 IT Pros & Developers. It's quick & easy.

Writing to a text file on the website server

P: 1
I want to be able to write text data to a (preexisting) text file on my website.

have readup heaps, and tinkered around a bit,
and it seems this might actually be possible afterall,
(because of the aweful security risk it poses) at least with javascript.

Can someone clear this up for me, is it possible
(and if so, how) ?

Cheers
Mike
Sep 29 '06 #1
Share this Question
Share on Google+
4 Replies


acoder
Expert Mod 15k+
P: 16,027
Not possible with JavaScript alone, but possible with Ajax. Use a server-side script to append to the file and use JavaScript to post to that script.
May 15 '08 #2

Expert 100+
P: 392
and it seems this might actually be possible after all
How?



Can someone clear this up for me, is it possible
Acoder, as usual, is correct. JavaScript is a client side language. Meaning it runs on the client, in this case the browser. It can not write to disk on a remote server. It can not even run on the server. HTML and or JavaScript can be used to send a file to the server, but you would need server side code there to receive it and write it to disk for you.

If you know of some way around this we are all ears.
May 15 '08 #3

rnd me
Expert 100+
P: 427
How?




Acoder, as usual, is correct. JavaScript is a client side language. Meaning it runs on the client, in this case the browser. It can not write to disk on a remote server. It can not even run on the server. HTML and or JavaScript can be used to send a file to the server, but you would need server side code there to receive it and write it to disk for you.

If you know of some way around this we are all ears.
[HTML]


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>javascript-only file writing</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<h1>javascript fileIO demo</h1>
<b>
javascript can use standard http methods like <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html">GET, PUT, and DELETE</a> to create, modify, and manage files on a remote server. The only configuration needed on the server, is the allowance of public or user-based write permissions.

this can be done on window iis server by right-clicking a folder in the IIS web site folder-view.
chmod can be used on unix machines.

if you use apache, then you will need a bit of server-side code called a <a href="http://httpd.apache.org/docs/1.3/misc/FAQ.html#putsupport">put handler</a> to catch the PUT requests. (<a href="http://hpwww.ec-lyon.fr/~vincent/apache/mod_put.html">module here</a>)
</b>
<br />
<hr />
<br />

open <a href='newPage.htm' target="_blank">my new page</a> in a new tab/window.
<br />
<br />

<textarea rows='10' cols='80' id='userValue'>Hello World</textarea>
<br />
<br />


<input type='button' value='Save' onclick="doSave()" />

&nbsp;&nbsp;&nbsp;
<input type='button' value='Load' onclick="doLoad()" />

<script type='text/javascript'>


function el(tid) {return document.getElementById(tid);}

function IO(U, V) {
//LA MOD String Version. A tiny ajax library. by, DanDavis
var X = !window.XMLHttpRequest ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
X.open(V ? 'PUT' : 'GET', U, !1);
X.setRequestHeader('Content-Type', 'text/html')
X.send(V ? V : '');
return X.responseText;}


function doSave(){
IO("newPage.htm" , el("userValue").value );
}

function doLoad(){
el("userValue").value = IO("newPage.htm");
}

</script>


</body>
</html>[/HTML]
May 16 '08 #4

acoder
Expert Mod 15k+
P: 16,027
Interesting, though it could pose a security risk, so should be for trusted users only.

This could be the subject of an article (hint, hint ;))
May 16 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.