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.

1
  • 1
    Why don't you just try it and see if it works? Commented Dec 14, 2009 at 23:27

10 Answers 10

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.

Sign up to request clarification or add additional context in comments.

1 Comment

From your post, I take it that there's no "generic" way to read in files? (Even using a plug-in such as Flash?)
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.

Comments

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

Comments

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.

Comments

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".

Comments

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.

Comments

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.)

2 Comments

It is important to note that while HTA can be a great application, it is also a MS technology and limited to Windows/IE
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.
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>

Comments

-4

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

Comments

-5

Yes, although insecure, you can use:

fopen("c:\\MyFile.txt", 'r');

2 Comments

In what browser can you run that code?
Sorry, that's not a standard function, I have used a custom function "fopen" that is rather long, apologies. phpjs.org/functions/fopen:774

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.