I was requested to find a way to enable users to launch (specific) local applications from their browser. Since the server cannot directly specify a file be opened on the client, it must be handled implicitly by the client O/S, yet be triggered from a browser - which is heavily weighted against interaction with the client system for security.
The company where I work has been moving almost all applications to web-based applications. There are, however, legacy application (typically C++, C#) that are used and require a desktop link to launch. The idea was to place the links within the browser to launch them. This, however, tip-toes around various security protocols built into the browser. There was, however, an indication that this was, under the proper circumstances, doable - as I can generate <XML> formatted Excel spreadsheets on the web server and the system opens them with Excel on the client.
Using the Code
In order to launch a local executable file from a browser link, one must induce the system to consider the local file to be a suitable and valid target for opening a file. Examples of this would be a .pdf file being opened by Adobe reader. The system is requested to open the file, as a link (or
Thus, we have our first hint into the procedure: the client O/S (and by proxy, the browser) need to be made aware of some sort of attempted file opening to require some specific local resource. We need to trigger an appropriate "
open-with" query from the system and assign it to our targeted local application. Unfortunately, except for the typical built-ins, it's not quite that easy.
The second part is where the work came in - for the browser is fortunately not that cooperative and will not launch an executable file just because you assigned it to a particular file extension. The browser will just try to open the file in a browser window and display it.
Rehashing, there are two steps:
- Setting up your client system to associate a specific file extension to a specific application on the client. In the example, the file's name is TEXT.BOGUS.
- Getting the browser to react to a file (link) by opening the application and not rendering the file. This requires the link be interpreted - using a script-language extension will convince the server to do this for you and at the same time, set up the target extension which then opens the desired application.
It turns out, step one is, as expected, pretty easy. You create a file and email it to yourself as an attachment. This part is an actual tip. It seems to work properly when induced by the email-"
open with" route but not with the Explorer "
open with" route.
Step two took the work, and although the content is simple enough, finding it and getting it together took a fair amount of searching to determine what needs to be done and then how to do it. As it turn out, the file you target with your link is opened by the browser, examined, and then properly disposed of. It is necessary, then, to convince the browser that the incoming item is a data file to be opened by an application and not a text file to be opened in the browser. This is accomplished by controlling the "
header" information with the following file content (for PHP):
header('Content-Disposition: attachment; '.'filename="TEST.BOGUS"');
header() sets up the browser for a binary data stream, such as you'd feed to many applications.
header() instructs the browser to handle the incoming data as an attachment (for external use) rather than inline (for browser use, such as displaying plain text). It also contains a file name whose extension informs the client O/S of the type of file coming and it then defaults to the assigned application to open it as the pre-selected option.
The above pre-selects opening a target file "TEST.BOGUS", which will appear in the Open / Save popup generated when you create the link. The above is the entire content of my target file and I named it header_test.php. I then used this file as the object of the various link methods. I do not link to TEST.BOGUS.
Points of Interest
The file to be opened in the browser is a script that creates header information that triggers the action. It contains a reference, internally, to a file type. This will trigger the initial offering in the open/save popup and thus target the application for your users.
The file type action is best assigned by emailing yourself the file with your custom extension as an attachment, opening that from the email and then assigning it to your executable as the default handler.
It is essential that there be no output (like ECHO) from your PHP prior to executing the
Another way to launch these, aside from a link and
location.assign() would be as the "
action" target of an HTML
If your application requires an initialization file, note that at the time of execution, your current working directory is your temp directory - you can put your .ini file there but this can be a problem if the folder is purged by the O/S on a regular basis.