简体   繁体   English

使用javascript,客户端打开文件

[英]Open a file using javascript, client-side

In my application I want to open a file that exists on a client machine. 在我的应用程序中,我想打开客户端计算机上存在的文件。 I created two applications: desktop and web application. 我创建了两个应用程序:桌面和Web应用程序。 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. 当用户安装桌面应用程序时,有一些文件被复制到其安装路径,我想通过javascript从我的Web应用程序打开这些文件。

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. 浏览器访问本地文件的唯一方法(除了沙箱内的文件,即cookie和缓存)是用户明确使用的HTML文件控件。

Unfortunately JavaScript doesn't have access to the client's file structure. 遗憾的是,JavaScript无法访问客户端的文件结构。 You could use something like Adobe AIR for your web application though maybe? 您可以使用Adobe AIR之类的东西作为您的Web应用程序吗?

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

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. 如果您可以使用JavaScript打开自己的文件,那么谁阻止我使用JavaScript打开您的文件。

The usual way to solve this problem is by using an ActiveX component and interacting with it through JavaScript. 解决此问题的常用方法是使用ActiveX组件并通过JavaScript与其进行交互。 This will limit you to IE though. 这会限制你到IE。

正如Oded上面提到的那样,出于安全原因,它是不可能的,如果你已经在客户端机器上安装了一个应用程序,那么你可以将参数传递给它并执行应用程序,在这种情况下你可以将文件的URI传递给应用程序,以便它在他们的机器上打开,但我无法在浏览器中看到它发生。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM