views:

55

answers:

2

In firefox 2 I was able to get the path using Browse - I use the path in our project to then write out files to that location. So now that browse does not get the path, does anyone know a way for a user to go to a directory and have the path returned via a web page so I could pass that along to the server for processing?

  • execCommand does not work in firefox and had limites save type capaility, and entering by hand is not a useable option. Thanks.
+1  A: 

The ability to see a complete client file path is now considered a security risk, and all modern browsers prevent you from seeing it (both via Javascript and via information sent back to the server on the form POST).

Pointy
Not what I hoped for, but thanks for your assistance
Dave.
+2  A: 

This is not possible with HTML/JavaScript. In HTML you can at highest use <input type="file"> to select a file, but not a folder or so. In JS you can't do anything at the local disk file system, let alone with a <input type="file"> element in the DOM tree. You're prohibited by security restrictions (you as being an enduser would of course not like if websites are able to do stuff at the local disk file system unaskingly).

You can only do that with a small application which runs straight at the client machine. For example a (signed!) applet which is basically just a piece of Java code served by a webpage which runs right at the client machine. You can communicate between applet and servlet using java.net.URL and consorts. Then, in the applet use Swing's JFileChooser to have a folder or file selection dialogue.

Update: by the way, MSIE and some other ancient browsers sends the full client-side disk file system path along the <input type="file"> to the server side. This is technically wrong (only the filename+extension should have been sent) and completely superfluous. This information is worthless in the server side, because it cannot access the file using the normal java.io.File stuff (unless both the server and the client runs at physically the same machine which of course wouldn't occur in real world). The normal way to get the uploaded file is to parse the multipart/form-data request body (for which one would normally use Apache Commons FileUpload or the Servlet 3.0 provided HttpServletRequest#getParts()).

BalusC
Thank you for your help and advice!
Dave.
I think that IE8 might "munge" the path, but I'm not sure; it definitely does in the client (i.e., when Javascript looks at the "value" of a file input). The way it does it is pretty funny of course.
Pointy
@Pointy: IE8 also falls in category "MSIE". Long live Commons IO: http://commons.apache.org/io/api-1.4/org/apache/commons/io/FilenameUtils.html#getName%28java.lang.String%29
BalusC
@BalusC well I do all my file uploads nowadays via Stripes, which wraps CommonsIO pretty cleanly. I basically don't have to mess with it at all.
Pointy
@Pointy: Stripes is just another nice lightweight MVC framework which takes all the nasty work from your hands ;) Kudos!
BalusC