2

How do I get the contents of a local file, say:

/home/user/Wired/uploads/1.csv

in a variable x in javascript?

Can I use the filereader http://www.w3.org/TR/file-upload/#dfn-filereader? If yes, then how?

Chuck
  • 998
  • 8
  • 17
  • 30
  • 1
    No. No you cannot. You cannot read the file system in client side javascript over the internet. – Raynos Dec 06 '11 at 14:10
  • This is Mozilla specific, so not an answer, but they've got a [new native Javascript file management API](http://dutherenverseauborddelatable.wordpress.com/2011/12/06/introducing-javascript-native-file-management/) that's pretty sweet. – kojiro Dec 06 '11 at 14:11
  • please specify if it's client side(browser) or server side javascript – Liviu T. Dec 06 '11 at 14:16
  • [Reading local files in JavaScript](http://www.html5rocks.com/en/tutorials/file/dndfiles/) – kojiro Dec 06 '11 at 14:17

2 Answers2

1

You can't do this with strict javascript, but you can use the web server as an intermediate between the user and the browser. Have the user upload the file asynchronously (using AJAX). The web server could then return the plain-text value back to the AJAX call. The contents would then be free to use as you see fit.

This is likely your only option without employing Flash/Silverlight/Java.

Chuck Callebs
  • 16,293
  • 8
  • 56
  • 71
0

Reading client files in javascript is possible indeed with the new File API available in modern browsers (I dont know if IE supports this now). Check this site and its code: http://www.readfileonline.com/ it allows you to read files using the browser only.

dev4life
  • 10,785
  • 6
  • 60
  • 73