I’m having some real issues in getting local files to open up via a browser using a custom protocol. Now before you shoot me for the custom protocol, this web app will be working in a closed intranet environment, so I have full control to add these protocols for all users. The app works fine at the moment, but I’ve been requested to bypass the Open / Save dialog for files as it’s ‘annoying’ and ‘time consuming’ (yeah, I know…I tried to tell them), so here I am in uncharted territory!
Here’s an example then of what’s not working (I’m using Notepad in this example to test it in Server 2008 R2 with IIS7):
Using the info on Registering an Application to a URL Protocol I’ve added the following to the registry:
HKEY_CLASSES_ROOT
opentxt
(Default) = "URL:opentxt Protocol"
URL Protocol = ""
DefaultIcon
(Default) = "C:\Windows\System32\notepad.exe,1"
shell
open
command
(Default) = "C:\Windows\System32\notepad.exe" "%1"
So now, in theory, it should be as easy as having the following link in my html
<a href="opentxt://localhost/openme.txt">Open File</a>
And it does almost work – click on the link and it opens up Notepad instantly, however I’m presented with this error in notepad.
The filename, directory name, or volume label syntax is incorrect
The file definitely exists in that location (I can read it using http: //localhost/openme.txt), and I’ve tried to represent the link in as many different formats, such as described here but just can’t get it to work.
Any ideas where I’m going wrong?
Quite simply, you’re confusing your two different protocols.
Your protocol – and the Url that you pass to notepad – is
opentxt://localhost/openme.txt. The working location of the file ishttp: //localhost/openme.txt. These two are not the same!HTTP is a protocol that is natively understood by the operating system and, as such, Notepad can pass the HTTP Url to the operating system, which will in turn connect to
localhostusing port 80 and send a request for that resource. The file is returned, and Notepad has its data. This is all handled within operating system libraries that define the behaviour of HTTP.Your
opentxtprotocol doesn’t have this support unless you code it. Notepad doesn’t know what to do with it – so you see your error message.A way to handle this easily is to create your own application that handles the protocol. If you had an
opentxtHandler.exewired up through the registry, it would be passed theopentxt://url, and can then process it appropriately. This might simply mean changing the protocol fromopentxttohttpand then passing it to Notepad – or it could be something more complex.