views:

369

answers:

9

Based on the other answers on this site, I already feel like I know the answer to this question, but, as it's slightly different, I wanted to ask.

Is it possible to access local files from JavaScript that is running locally on the machine (AKA, my website address will be file:///C:/...)? Or, is this sandboxed as well?

What I am trying to do: I have a standalone computer that I want people to be able to drop in JSON or XML files into a local folder which are read in at the creation of the site and used to generate a single web page. If the JavaScript solution is not possible, can you provide any other suggestions?

Thank you.

A: 

If people are to drop a json string in to a folder, you could just have it be a plain text file, then use an AJAX call to the file name, just like you would point it to a php/asp script. I do this all the time for testing of pages before I have the backend done.

I.E. if your page were C:\foo\index.html, you could have them drop to C:\foo\putyourstuff\json.txt here and run an AJAX call "putyourstuffhere/json.txt".

Chris Sobolewski
A: 

IF the user grants your webpage permission to access those files, and IF they are located on the same machine as the webpage, then there is nothing preventing you from gaining Read Only access to files on the machine via JavaScript.

Josh
A: 

You could read the files using just an Ajax request, as though it was to the server. But you have to know the name of the file, and you can't write files.

Matthias Wandel
A: 

Yes, although insecure, you can use:

fopen("c:\\MyFile.txt", 'r');
jnunn
In what browser can you run that code?
Marius
Sorry, that's not a standard function, I have used a custom function "fopen" that is rather long, apologies.http://phpjs.org/functions/fopen:774
jnunn
A: 

You should consider some sort of server side scripting language such a PHP, Perl, JSP or some form of SSJS.

Kitson
A: 

If you make an hypertext application page (.hta) instead of an HTML page (.htm/.html), then you have full access to the file system, using the FileSystemObject object.

(Well, limited by the file access of the user account that is running the browser, of course.)

Guffa
It is important to note that while HTA can be a great application, it is also a MS technology and limited to Windows/IE
Josh
That being said... if you know you are only ever going to use this on Windows/IE then HTA is probably a really good option.
Josh
+1  A: 

This will only work on IE, but if that is not a problem for you, here is some sample code to write to a file:

    var fso, s;
    fso = new ActiveXObject("Scripting.FileSystemObject");
    s = fso.OpenTextFile("c:\\path\\to\\myfile.txt" , 8, 1, -2);
    s.writeline("Hello World");
    s.Close();

And then to read from it:

f = fso.OpenTextFile(filename, ForReading);
while (!f.AtEndOfStream) {
    var r = f.ReadLine();
    document.write (r + "<br />");
}
f.Close();

For more information about OpenTextFile, check out: http://msdn.microsoft.com/en-us/library/314cz14s%28VS.85%29.aspx

pkaeding
+3  A: 

A webpage can read any file on the same server as it was loaded from (this is the cross-site policy of JavaScript). That means that the page file:///C:/mywebsite/index.html can read the file file://C:/somedir/somefile.xml. To read this file, either use ajax, load it in an iFrame or load it as a javascript or css file.

Several browsers support custom methods for loading local file (and other interesting things), IE has activeX and Firefox has XPCOM.

Marius
From your post, I take it that there's no "generic" way to read in files? (Even using a plug-in such as Flash?)
JasCav
+1  A: 

According to the Firefox documentation, the following code will work:

var req = new XMLHttpRequest();  
req.open('GET', 'file:///home/user/file.json', false);   
req.send(null);  
if(req.status == 0)  
  dump(req.responseText);

I seem to recall it only works within the same directory as the HTML page. And I don't know if this will work in other browsers.

Jason Orendorff