0

How do I read a local text file in JavaScript, w/o jquery etc.? I realize the question has been asked before, with a gazillion answers (but both html and javascript also have changed a lot over the years).

So here is my scenario:

  • do not want to use any webserver for this purpose as I wish to send a tarball+make install to someone for a demo and want zero installation
  • a blocking read is fine
  • files are simple text files such as "downloaded/xyz.json". They are indeed stringify'd json. Do not want to use a files dialog box to select file name - hardcoding is the desirable for this purpose
  • Security is not an issue in this context, although I am not prepared to run the browser with --disable-web-security or do anything that's even remotely cross-domain
  • env: linux (openSuse 11.4), using google-chrome
  • do not want to use jquery for this

Will greatly appreciate a simple code snippet, if it is feasible.

styfle
  • 22,361
  • 27
  • 86
  • 128
Dinesh
  • 4,437
  • 5
  • 40
  • 77
  • jquery can't do anything that javascript can't... it's just a helper library to paper over missing bits of the JS spec and/or inter-browser bugs. – Marc B Aug 27 '12 at 20:48
  • 1
    Will they be loading the page locally? Can you modify the `.json` files to say `var whatever = {somejson}`? If so, just reference your files with relative paths. – Brad Aug 27 '12 at 20:48
  • possible duplicate of [Chrome FileReader](http://stackoverflow.com/questions/4100927/chrome-filereader) – epascarello Aug 27 '12 at 20:49
  • Checkout the [HTML5 draft File API](http://www.w3.org/TR/FileAPI/) and some [interesting demo code](http://www.html5rocks.com/en/tutorials/file/dndfiles/). – maerics Aug 27 '12 at 20:50
  • possible duplicate of [Reading file contents on the client-side in javascript in various browsers](http://stackoverflow.com/questions/750032/reading-file-contents-on-the-client-side-in-javascript-in-various-browsers) – Andreas Köberle Aug 27 '12 at 20:52
  • @maerics: how does one instantiate a File object from a simple Unix pathname? I tried something like new File("file:///tmp/foo") and also super-optimistic new File("/tmp/foo") but they both give error "Illegal Constructor" :( – Dinesh Aug 28 '12 at 02:51
  • @Brad: Can you possibly help out a little more? I construct the file name on the fly using user inputs in a form. For example, the user may enter an entity name in a form field and I want to read (entity_name + "_contracts.json"). So using – Dinesh Aug 28 '12 at 03:05
  • @user1397919, Of course the script tag can work for you. Just append an element to the page dynamically. This is exactly the same concept behind JSON-P. – Brad Aug 28 '12 at 03:10
  • @user1397919: I don't think it is (or will ever be) possible to construct a File/FileReader without explicit user interaction (such from a UNIX pathname) since it would be a security issue. What would prevent malicious web pages from reading your email, or your password file? – maerics Aug 28 '12 at 12:57
  • @user1397919, I assume you are being sarcastic. I don't know how else to help you until you answer my questions. Are they loading this page locally (direct from the filesystem) or not? Are you able to modify these files or not? – Brad Aug 28 '12 at 13:58

0 Answers0