3

In my application I want to open a file that exists on a client machine. I created two applications: desktop and web application. When the user installs the desktop application there are some files which is copied to its installation path, and I want to open those files from my web application via javascript.

David Tang
  • 92,262
  • 30
  • 167
  • 149
BreakHead
  • 10,480
  • 36
  • 112
  • 165
  • Javascript is sandboxed within the browser, this isn't possible. – Lazarus Feb 25 '11 at 09:26
  • @Lazarus It was not possible a few years ago, but some browsers now support the [File System Access API](https://developer.mozilla.org/en-US/docs/Web/API/File_System_Access_API). – Anderson Green Feb 23 '22 at 23:18

5 Answers5

4

The browser is insulated from the host machine (sandboxed) for security reasons.

The only way for the browser to access local files (apart from those inside the sandbox, ie. cookies and cache) is the HTML file control used by the user explicitly.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
0

Unfortunately JavaScript doesn't have access to the client's file structure. You could use something like Adobe AIR for your web application though maybe?

http://www.adobe.com/devnet/air/flex/articles/exploring_file_capabilities.html

JamesHalsall
  • 13,224
  • 4
  • 41
  • 66
0

The browser is sandboxed against that very scenario. If you can open your own files using JavaScript, who's to stop me from opening your files using JavaScript.

The usual way to solve this problem is by using an ActiveX component and interacting with it through JavaScript. This will limit you to IE though.

jjrdk
  • 1,882
  • 15
  • 18
0

As Oded has mentioned above for security reasons its not possible, having said that If you have an application installed already on the clients machine then you can pass parameters to it and execute the application, in this case you would pass the URI of the file to the application so that it opens on their machine but I cant see it happening with-in the browser.

RaM
  • 1,126
  • 10
  • 25
-1

what about this?

<script>
var oRequest;

if(document.all) {
   // Internet Explorer
   oRequest = new ActiveXObject("Microsoft.XMLHTTP")
}
else {
   // Mozilla
   oRequest = new XMLHttpRequest();
}


oRequest.open("GET", "file:///C:/myLocalFile.txt", false);
oRequest.send(null);
textToBeWritten = oRequest.responseText;

document.write(textToBeWritten);
</script>
profanis
  • 2,741
  • 3
  • 39
  • 49