2

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:

  1. 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.

  2. When writing a file, is it possible to write to an existing file or does the process simply destroy and recreate the same file?

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
Katana24
  • 8,706
  • 19
  • 76
  • 118
  • 3
    So... node.js? Otherwise, JavaScript doesn't really run on the server. – Ry- Feb 09 '12 at 23:19
  • javascript on the client side (browser) or server side (e.g. with node.js)? – Hamish Feb 09 '12 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 Feb 09 '12 at 23:20
  • You should specify in which environment your JS is running. Browsers or server-side JS environments like node.js – raphinesse Feb 09 '12 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 Feb 09 '12 at 23:21
  • Similar: http://stackoverflow.com/questions/7160720/create-a-file-using-javascript-in-chrome-on-client-side – Travis J Feb 09 '12 at 23:29
  • Ok allow me to clarify - my knowledge of javascript is limited therefore I don't know how it worked. The app i am creating will make contact with the server therefore it has to be online but what I meant was I will be the only one accessing it - no one else. The javascript program I have does some complex math which would be too bulky to run on the phone therefore it runs on the server and writes the values from the code to a text file on the server. The phone contacts the server to get this text file. So it will be running on a server side environment. Is this clear? – Katana24 Feb 10 '12 at 10:22

2 Answers2

3

I would use PHP

http://www.php.net/

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:

http://api.jquery.com/jQuery.ajax/

SSH This
  • 1,864
  • 4
  • 23
  • 41
  • I'm of the opinion that the question is too unclear to say if PHP might be appropriate or not … but even if it wasn't, this answer ignores most of the specific queries in it. – Quentin Feb 09 '12 at 23:23
  • Duly noted, I added some further information, hopefully this will help – SSH This Feb 10 '12 at 17:33
2

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.

Muhammad Abrar
  • 2,292
  • 1
  • 15
  • 13
  • There's been no mention of HTML being involved. The question talks about a server and an Android application. – Quentin Feb 09 '12 at 23:24