Saving data to a local file using only JavaScript

13,388

Solution 1

You can use the Chrome Apps File API, you will need to grant access to the file via a user action once, but after that you can get access the file again by using restoreEntry

Solution 2

You could use HTML5?

http://diveintohtml5.info/storage.html

var foo = localStorage.getItem("bar");
// ...
localStorage.setItem("bar", foo);

Solution 3

You can use localStorage to save offline data. It's impossible to access other files using Javascript since it's a violation of the sandbox.

From http://en.wikipedia.org/wiki/JavaScript#Security:

JavaScript and the DOM provide the potential for malicious authors to deliver scripts to run on a client computer via the web. Browser authors contain this risk using two restrictions. First, scripts run in a sandbox in which they can only perform web-related actions, not general-purpose programming tasks like creating files.

Solution 4

You may want to look into Local Storage which is part of the HTML5 spec. This will only be supported in modern browsers though.

If you need to use older browsers then may still be able to achieve what you're after using dojox.storage

Solution 5

Use HTML5 features like Web Storage or Web SQL database to store your logs.

Whenever needed read logs from the client side storage and send it back to the server & delete the client storage.

Refer http://www.html5rocks.com/en/features/storage.

Share:
13,388
sidonaldson
Author by

sidonaldson

Updated on August 04, 2022

Comments

  • sidonaldson
    sidonaldson over 1 year

    The set-up in question:

    I have a stand alone, offline, kiosk mode instance of Chrome running on a Windows machine. I have full access to the system and any admin rights. I can start Chrome with any flags set or unset.

    The task:

    I have been asked to create a log file which tracks user activity within the offline app I am coding. It's a simple form of analytics which will append each event to the end of the file separated with a comma. The file will then be sent to a server once a day via a scheduled task. (None of this is my idea so please don't troll me)

    Ruled out: Any server side code - I have lobbied for Node, PHP etc but as this will be distributed to many different installations so we cannot guarantee this will be installed. Flash/ActiveX/Java - ideally would like to avoid this (even though these will be installed by default)

    Possible solutions: File API - I have looked at this but AFAIK if opens dialogue boxes to save the data to each file and this needs to happen in the background. Security - I have read in other SO Questions that this can be achieved if the security settings are reduced but no-one goes on to explain which ones. Is there a simple flag which allows it? How to enable local file system read write access from Google chrome? - similar question!

    Ideal result: (something akin to PHP)

    $file = 'log.txt';
    $current = file_get_contents($file);
    $current .= ",clicked:link";
    file_put_contents($file, $current);
    

    Possible ideal side result: proving this isn't possible and forcing PHP/Node/Java to be used ;)

    In reply to those suggesting local storage : I'm not storing unique key/value pairs and that is very much like setting a cookie. Similarily there are file size limits. To those suggesting web SQL such as SQLite in chrome - there are file size limits if it's not a chrome extension. The only way I see that working is if I were to find the location of the file in the windows directory (C:\Users\%USERNAME%\AppData\Local\Google\Chrome\User Data\Default\databases) and upload that from the schedules task. Perfectly feasible but it is not a desirable answer.

  • sidonaldson
    sidonaldson over 10 years
    This sounds promising. Is it only available for Apps and extensions? I've found this Angular module that appears to wrap it up: github.com/maciel310/angular-filesystem/blob/master/src/…
  • Kinlan
    Kinlan over 10 years
    @sidonaldson - Only to Apps at the moment. Note, that angular module only wraps the sandboxed API, this API in my answer is to get access to the raw filesystem.
  • sidonaldson
    sidonaldson over 10 years
    As I didn't specify this cannot be an app I am more than happy to accept this as the solution. Do you know or a example case - study or similar?