Linking to local content in a web application

2019-01-29 08:30发布

I am working on a replacement application to a legacy application. Due to certain design limitations of the legacy application, 'attachments' are stored as a String path in our database (generally files stored on a windows shared drive). The legacy application can then 'open' the attachments by opening a windows command shell and executing the given path.

The legacy (Oracle Forms) application is being phased out by a JSF based J2EE web application. The new application needs to be able to 'open' or link to these legacy attachments somehow. Is this even possible? I have attempted to use file:// URLs, but there are lot of caveats with using them. They only work on remote hosts in IE, firefox/chrome (and other modern browser I assume) prevent local file URLs.

Working only on IE is something that can be lived with for this particular feature. I further ran into an issue with file paths with spaces. For some reason if IE encounters a filepath with spaces in it, say

C:\Documents and Settings\user123\My Documents\testing\someFile.txt

it refuses to open that link. The browser automatically replaces the spaces (' ') with its URL Encoded '%20'.

The associated link I am attempting looks like:

<a href="file:///C:\Documents and Settings\user123\My Documents\testing\someFile.txt">link</a>

Is there something simple to this I am missing? Or is there any easier way of doing this?

I wasn't quite sure what to tag this as so feel free to retag as necessary.

2条回答
唯我独甜
2楼-- · 2019-01-29 08:55

If you use double quotes (") around the path to the file (you will probably need to URL encode these as %22), windows will be OK with the full path:

<a href="file:///%22C:\Documents and Settings\user123\My Documents\testing\someFile.txt%22">link</a>
查看更多
在下西门庆
3楼-- · 2019-01-29 09:02

After some extensive testing I have reached the following conclusions:

  • Only IE will open file:// links that are on a page from a remote host, Other browsers will block them outright and nothing will happen when a user clicks on them.
  • IE will only open file:// URLs that point to a file that resides on a network drive
    • If a user clicks on a file:// link pointing to a file on the user's local drive, nothing will happen and they will get no error.
  • If a user clicks on a file:// link that points to a network file it will open in the browser, if possible.
  • If the file:// url points to an invalid location (unmapped network drive, file on a network drive that doesn't exist), Windows will show a popup error.
  • Spaces in the file path needs to be URL encoded with %20

Hopefully this helps someone else out who's looking for information on file urls.

查看更多
登录 后发表回答