Can Javascript access files on the server?

Someone asked about How to connect to SQL server database from javascript? and I found it possible,

So, can someone surprise me and say yes to my question?

Can Javascript access files on the server?

I know this is not possible, but I said NO in the sql database question, but I found someone answer yes to the question.

-2


a source to share


8 answers


The question you linked does not use JavaScript to access a SQL database.

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



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

+4


a source


Usually there is no chance.

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



Below is an example of a .hta page using an object FileSystemObject

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>

      

+5


a source


Quite right. If you are using server side javascript. :) From a browser? I think not, and you wouldn't want that. There's a lot of tricks out there using ajax, flash, or whatever to get information into the browser DOM, but you want to set up a script, web service, endpoint, or other ways to expose data to the server, and then fetch that from that.

+3


a source


SQL From Javascript on the client? Are you kidding, right?

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

+2


a source


You can use AJAX to communicate with the server, but not directly access resources such as files, databases.

+2


a source


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

You can create a web service on your server that will return data from a file using ajax, like the contents of a text file. Reading / parsing the file would be the responsibility of the web server, although it would have to pass the payload to the client.

You won't be able to get finer control over the file than this.

+1


a source


Opening your server's filesystem on the server side script is likely to cause a lot more complications, which are worth it. In fact, people have asked me this question before, and each time this person just didn't know any better; they didn't understand web development to know how to do something like that, they were just trying to apply their desktop programming experience to web development. There are probably better ways to do what you are trying to do.

+1


a source


In theory, you can do anything in JavaScript, because you can use COM objects (at least on Windows), which in turn can do almost anything on that machine. In reality, if this JavaScript is executed from a web page, you will face countless security / permission / firewall issues, so it really isn't worth convincing.

0


a source