views:

1574

answers:

8

Some one asked about How to connect to SQL server database from javascript? and i found thats its possible,

So some one can surprise me and say yes on my question?

Can Javascript access files on the server?

I know its not possible, but i said NO on the sql database question, but i found someone answering yes !!

+2  A: 

SQL From Javascript on the client? You're kidding, right?

As for files on the server... welcome the the web. Perhaps your question needs further clarification.

spender
+1  A: 

You can use AJAX to talk to the server, but not directly access resources such as files, Databases.

Marwan Aouida
+1  A: 

It can but not in the traditional open file, read file and close file paradigm.

You could create a web service on your server that would return data from a file using ajax, such as the contents of a text file. The reading/parsing of the file would be the web server's responsibility though, and it would then have to pass the useful data back to the client.

You won't be able to get any finer control over the file than that though.

Neil Trodden
+5  A: 

Generally, no chance.

It would be possible under very special circumstances. The page would have to be a hypertext application (.hta) to be able to access the file system at all, and the server would have to be in the same local network as the client.

Here is an example of an .hta page using the FileSystemObject object to read a file from the server:

<html>
<head>
<title>File</title>
</head>
<body>
<script>

var f = new ActiveXObject("Scripting.FileSystemObject");
var name = '\\\\TheServer\\Users\\Public\\Downloads\\test.txt';
if (f.FileExists(name)) {
    var stream = f.OpenTextFile(name, 1);
    var text = stream.ReadAll();
    stream.Close();
    alert(text);
} else {
    alert('File could not be accessed.');
}

</script>
</body>
</html>
Guffa
A: 

Opening up your server's file system through a server side script would likely open up far more complications that it's worth. I've had people ask me this question before, actually, and every single time that person simply didn't know any better; they didn't understand web development enough to know how to do something like this, they were simply trying to apply their experience with desktop application programming to web development. There are likely better ways to accomplish whatever you're trying to do.

Andrew Noyes
+3  A: 

Absolutely. If you're running javascript on the server-side. :) From a browser? No, nor would you want to, I think. There's lots of tricks using ajax, flash or whatever else to get info into the browser DOM, but you'll want to set up a script, web service, endpoint, or other means of making data available on the server, then fetch it from that.

jess
+2  A: 

The question you refer do doesn't use JavaScript to access the SQL database.

It uses JavaScript to access an ActiveX Control (which requires Internet Explorer + Widows + Heightened Security Privileges).

You can access the file system in the same way - given a suitable ActiveX control.

David Dorward
A: 

In theory, you can do anything in JavaScript because you can use COM objects (on Windows at least), which in turn can do pretty much anything on that machine. In reality, if this JavaScript is executed from a web page, you will run into countless security/permission/firewall problems, so it's really not worth persuing.

JonoW