I want to write to a text file using javascript. I know that it is possible but there are some problems.
I am running a javascript program that calculates the location of an object (its latitude & longitude) which changes every 5 seconds; i want to write this information to a text file. The javascript program will soon run on a server and I'll use the information written to the text file to communicate with an Android app on my phone.
So, my question really is:
How can it be done properly?
I know that there may be some permission issues but considering it won't be online and available to others will it be a problem and, if it is, should I go with PHP to do what I want? I know ASP is more Microsoft orientated and I work with a Mac so PHP would be the preference here.When writing a file, is it possible to write to an existing file or does the process simply destroy and recreate the same file?
-
3So... node.js? Otherwise, JavaScript doesn't really run on the server.Ry-– Ry- ♦2012年02月09日 23:19:59 +00:00Commented Feb 9, 2012 at 23:19
-
javascript on the client side (browser) or server side (e.g. with node.js)?Hamish– Hamish2012年02月09日 23:20:00 +00:00Commented Feb 9, 2012 at 23:20
-
I think you might want to clarify your question. If the JavaScript programme is running on the server then you'll need to be running a JS engine there (probably Node), which would remove the need for a second server side language (such as PHP or one that is ASP compatible)Quentin– Quentin2012年02月09日 23:20:49 +00:00Commented Feb 9, 2012 at 23:20
-
You should specify in which environment your JS is running. Browsers or server-side JS environments like node.jsraphinesse– raphinesse2012年02月09日 23:20:51 +00:00Commented Feb 9, 2012 at 23:20
-
If it isn't "online" then where is it? Are you planning a server-client app with both server and client running on a local network? Or is the entire thing running on the phone? (In which case, why is there a server involved?)Quentin– Quentin2012年02月09日 23:21:36 +00:00Commented Feb 9, 2012 at 23:21
2 Answers 2
I would use PHP
This has a good code example:
http://www.tizag.com/phpT/filewrite.php
Also, you can make the request using jQuery's AJAX function, this will allow to effectively run this code from javascript:
Using the latest HTML5 Javascript, you can use the FileSystem APIs to read/write/append to text files. This is a good tutorial here : Exploring the FileSystem APIs.