11

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.

robjmills
  • 18,438
  • 15
  • 77
  • 121
JasCav
  • 34,458
  • 20
  • 113
  • 170

10 Answers10

5

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
  • 57,995
  • 32
  • 132
  • 151
  • 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 Dec 15 '09 at 20:28
5

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
  • 42,793
  • 6
  • 62
  • 96
2

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(VS.85).aspx

pkaeding
  • 36,513
  • 30
  • 103
  • 141
1

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
  • 44,706
  • 7
  • 102
  • 124
0

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
  • 12,819
  • 12
  • 63
  • 96
0

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
  • 6,383
  • 10
  • 33
  • 31
0

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
  • 687,336
  • 108
  • 737
  • 1,005
  • 4
    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 Dec 14 '09 at 22:48
  • 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 Dec 14 '09 at 22:48
0

This worked for me in Firefox...

Adapted code from this site: http://www.h-online.com/security/services/Firefox-Mozilla-Demo-Reading-local-files-via-local-HTML-files-761851.html

<HTML>

<BODY onLoad="ReadFileContent()" >

<iframe id="local_file" name="local_file" 
    src="file:///C:/test.txt"
    height=0 width=0>
</iframe>

<script>

function ReadFileContent(){

alert(local_file.document.firstChild.innerHTML);

}

</script>

<h2>Read Local File</h2>

<P>
If a window displays the content of your local file C:\test.txt
the demo worked. If no additional window appears, it failed. You can
close this window now.</p>

</body>

</html>
Teddy Katayama
  • 128
  • 1
  • 9
-4

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

Kitson
  • 1,650
  • 1
  • 18
  • 36
-5

Yes, although insecure, you can use:

fopen("c:\\MyFile.txt", 'r');
jbnunn
  • 6,161
  • 4
  • 40
  • 65
  • In what browser can you run that code? – Marius Dec 14 '09 at 23:07
  • 2
    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 – jbnunn Dec 14 '09 at 23:44