从 html 页面执行 exe 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5506244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Execute exe-file from html-page?
提问by Hedge
I want to launch a local exe-file (without saving it to another location first) upon clicking on a link on a local html file.
我想在单击本地 html 文件上的链接时启动本地 exe 文件(无需先将其保存到其他位置)。
It either needs to work in IE, Firefox, Chrome or Opera, I don't care. It's just for a presentation tomorrow.
它要么需要在 IE、Firefox、Chrome 或 Opera 中工作,我不在乎。只是为了明天的演示。
采纳答案by Franti?ek ?ia?ik
It's simply not possible. If it was, it would be considered a security flaw and fixed. On Firefox within hours, on IE within some months.
这根本不可能。如果是,它将被视为安全漏洞并已修复。几个小时内在 Firefox 上,在几个月内在 IE 上。
UPDATE: You could try registering your custom protocol: http://openwinforms.com/run_exe_from_javascript.html
更新:您可以尝试注册您的自定义协议:http: //openwinforms.com/run_exe_from_javascript.html
But I believe the browser will still prompt you whether you want to run the app.
但是我相信浏览器还是会提示你是否要运行该应用程序。
回答by Coskun Ozogul
I want to share my experience.
我想分享我的经验。
The accepted response says that it is not possible but it is quite possible indirectly.
接受的回应说这是不可能的,但间接地很有可能。
If you want to execute an exe on a pc, it means that you have acces on this pc and you can install your exe on that machine.
如果你想在电脑上执行一个exe,就意味着你在这台电脑上有访问权限,你可以在这台机器上安装你的exe。
In my case, I had to take a 3D scan from a 3D scanner via a web application. It seemed impossible at the beginning.
就我而言,我必须通过 Web 应用程序从 3D 扫描仪进行 3D 扫描。一开始似乎是不可能的。
After lots of research, I found that we can send socket messages via javascript.
经过大量研究,我发现我们可以通过 javascript 发送套接字消息。
It means that if we had an application which listens a specific port, it can communicate with a website.
这意味着如果我们有一个监听特定端口的应用程序,它可以与网站进行通信。
Let's explain how I did this.
让我们解释一下我是如何做到的。
In my web application, I created a javascript method like this :
在我的 Web 应用程序中,我创建了一个这样的 javascript 方法:
function openCapron3DScanner(foot) {
$("#div-wait").show();
//Creates a web socket pointed to local and the port 21000
var ws = new WebSocket("ws://127.0.0.1:21000");
ws.onopen = function () {
//Sends the socket message to the application which listens the port 21000
ws.send(foot + "-" + @ProjectHelper.CurrentProject.Proj_ID);
};
ws.onerror = function myfunction() {
$("#div-wait").hide();
alert("Erreur connection scanner.");
}
ws.onmessage = function (evt) {
//Receives the message and do something...
var received_msg = evt.data;
if (received_msg == "ErrorScan") {
alert("Erreur scan.");
}
else {
refreshCurrentProject();
}
};
ws.onclose = function () {
$("#div-wait").hide();
};
};
And I created a windows forms application who listens the localhost and port 21000. This application is hidden, only shown in icon tray. The only thing to do is to add the application on windows startup via code on the first load to assure that the next restart of windows it will be executed and listen the port.
我创建了一个 windows 窗体应用程序,它监听本地主机和端口 21000。这个应用程序是隐藏的,只显示在图标托盘中。唯一要做的就是在第一次加载时通过代码在 windows 启动时添加应用程序,以确保下次重新启动 windows 时将执行它并侦听端口。
private static WebSocketServer wsServer;
static WebSocketSession LastSession;
private void Form1_Load(object sender, EventArgs e)
{
wsServer = new WebSocketServer();
int port = 21000;
wsServer.Setup(port);
wsServer.NewMessageReceived += WsServer_NewMessageReceived;
wsServer.Start();
}
private static void WsServer_NewMessageReceived(WebSocketSession session, string value)
{
if (value.StartsWith("ScanComplete-"))
{
//If the scan is ok, uploads the result to the server via a webservice and updates the database.
UploadImage(value);
//Sends a confirmation answer to the web page to make it refresh itself and show the result.
if (LastMacSession != null)
LastMacSession.Send("ScanComplete");
}
else if (value == "ErrorScan")
{
//If the C++ application sends an error message
if (LastMacSession != null)
LastMacSession.Send("ErrorScan");
}
else//call the 3D Scanner from the web page
{
LastSession = session;//Keeps in memory the last session to be able to answer via a socket message
//Calls the C++ exe with parameters to save the scan in the related folder.
//In could be don in this same application if I had a solution to consume the scanner in C#.
var proc = System.Diagnostics.Process.Start(@"C:\Program Files\MyProjectFolder\MyScannerAppC++.exe", projectID + " " + param);
}
}
I hope it will help.
我希望它会有所帮助。
回答by karthick
You can't run an exe file on a website. (First, if it's a Linux server, exe files won't run on it and second, if you're on a Windows server, your host would kill the program immediately. And probably terminate your account.)
您不能在网站上运行 exe 文件。(首先,如果它是 Linux 服务器,exe 文件将不会在其上运行;其次,如果您使用的是 Windows 服务器,您的主机会立即终止该程序。并且可能会终止您的帐户。)
That link (assuming it was Play Now!) will just allow your user to download the file. (C:\Program Files\World of Warcraft\ exists on your computer, but it doesn't exist on the web server.)
该链接(假设它是立即播放!)将只允许您的用户下载文件。(C:\Program Files\World of Warcraft\ 存在于您的计算机上,但它不存在于 Web 服务器上。)
回答by Dominik Antal
You could setup a custom protocol on your local OS, if it's Windows, in regedit. Check out thisand this.
您可以在本地操作系统(如果是 Windows)上的 regedit 中设置自定义协议。看看这个和这个。
Then you create a simple HTML page, and place a link, something like this :
然后创建一个简单的 HTML 页面,并放置一个链接,如下所示:
<a href="presentation://C:\start.exe/">Start!</a>
Given that you registered your custom "presentation" protocol, and configured it correctly in the registry, the application should launch when you click that link.
鉴于您注册了自定义“演示”协议,并在注册表中正确配置了它,当您单击该链接时,应用程序应该会启动。
回答by John Batdorf
Use System.Diagnostics.Process.Start() method.
使用 System.Diagnostics.Process.Start() 方法。
protected void LinkButton1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("notepad.exe");
}
You'll have to use C#, but since that's on your post, it should work. You'll also need the full path, if the file is not in your environment path that's loaded in memory.
您将不得不使用 C#,但由于这是在您的帖子中,它应该可以工作。如果文件不在内存中加载的环境路径中,您还需要完整路径。
For a 'regular link' you'd still need to place this in an ASPX page.....
对于“常规链接”,您仍然需要将其放在 ASPX 页面中.....
<a href="test" onclick="<% System.Diagnostics.Process.Start("notepad.exe"); %>">Click me</a>
<a href="test" onclick="<% System.Diagnostics.Process.Start("notepad.exe"); %>">点击我</a>
We're getting really fugly now though.
不过,我们现在变得非常糟糕。