views:

64

answers:

3

I have a HTML file upload field from which I'm reading the file name of the file that the user specifies. The actual contents of the file is never uploaded.

At a later stage, is it possible to construct a link using this file name information so that if the user clicks on this link, the original file is launched into a new browser window? If not, what are the reason for disallowing this behaviour?

The purpose of such a feature is to store links to documents that are available on a mapped local drive or a network share.

+2  A: 

New answer

Since Andreas pointed out (quite kindly!) that my old answer (below), while correct, was almost certainly useless, I thought I'd take another swing at it with something I only learned about a few days ago: The new File API from the W3C.

You won't be able to make much use of this now (except on Firefox 3.6 or above), but before too long it'll find its way into modern browsers. It actually gives you access to local files, as long as the user has explicitly selected the file for you (via an input type="file" element). You still can't see their paths, but you can open and read them from the browser (yes, really). So you could open a new window, open the file, and write the contents to the new window. Whether that will really do what you want is another question (you'll have fun writing binary files to a new window, for instance, even though the File API lets you read them just fine), but hey, it's there.

Old answer

(Although you can do the below, Andreas points out that it won't work on just about any browser unless the page the link is in is also served via a file:// URL. So, not much use then.)

You should be able to use a file:// URL for that, e.g., c:\test.txt becomes file:///C:/test.txt. Then the link just uses the URL and the target="_new" attribute telling the browser to open a new window/tab, e.g.:

<a href='file:///C:/test.txt' target='_new'>link to test.txt</a>
T.J. Crowder
+4  A: 

Most likely, no.

T.J.'s Crowder answer is correct but do note that on virtually all modern browsers that will work only if the webpage you are viewing (with the input type=file) is itself a file:///.

From your question it appears to be some kind of internal company website, which most likely will have its own web server.

Andreas Bonini
Yes, it runs on an internal server. Does this mean that the browser will block a link going to file:/// style link?
Pieter Breed
Pieter: Yes. ` `
Andreas Bonini
@Andreas: Thank you for that!
T.J. Crowder
+2  A: 

You'll notice that in newer web browsers your client-side code will not actually be able to read the complete pathname of a file chosen by a user. That's a security measure. IE8 provides you with some sort of obviously fake directory name, while Firefox and the Webkit browsers just strip off everything other than the file name.

Thus, I don't think you'll be able to do what you want, at all.

edit: info from msdn: http://msdn.microsoft.com/en-us/library/ms535128%28VS.85%29.aspx

Pointy
Thanks! This is exactly the kind of answer I am looking for. Is there some kind of reference information for this? You say IE8 provides a fake path? Do you know if this is documented somewhere?
Pieter Breed
I think I found documentation for it not too long ago; I was setting up a hack to re-style file input fields and I came across the silly "fakedir" thing that IE returns. I'll see if I can find that reference again.
Pointy