2008-09-20

Get file path in Firefox 3 - revisited

Because my previous entry on the subject is one of the most popular entries in my blog, yet the solution is not satisfactory (you have to get your users to mess with about:config), i decided to take a second look at the problem.

Just as a reminder: in Firefox 3 when you try to access a file input's value property, it only gives you the filename, whereas earlier versions (and some other browsers at the moment) would give you the full path.

Firefox team considers this as a security patch and there is a fair chance that IE8 (and ultimately other browsers) will have the same behavior.
There is a lot of discussion about this here and here, and apparently a lot of applications have been relying on this feature. (The above links are bug reports, try to refrain from useless comments)

A new approach to the problem lies in Adam Fisk's comment here. We can use a java applet to get the file path! The solution is working at the moment in all major browsers (demo here), but it might also be disabled in the future. Another problem is that because it completely avoids using the file input, you will still need to have your applet to do the actual upload. If you only need the path, you are fine, but if you need the path AND the file on the server side, you will need some more programming.

27 comments:

G.Marchetto said...

I tested in my website the demo code, but it doesn't work in IE7 either in FF3 (Windows XP).
Applet starts buts clicking on the button it generates a java.lang.extension error (Ie7): the metod doesn't exist

maschek said...

what is the version of your java engine?

Anonymous said...

Hi,

on the demo page it says "...see the source for implementation details" but where can I get the sources?

Cheers,
Daniel

maschek said...

@Daniel

The javascript source you can see in the html and in http://maschek.hu/preview/ffx3_file/appletLoader.js.

The applet source used to be linked from:
https://bugzilla.mozilla.org/show_bug.cgi?id=405630#c40

But seems like Adam removed it, and i dont have the original, just the class file. Request from him the source.

Anonymous said...

I'm having a hard time implementing this. I have the HTML, javascript and class file. However all I get is a message that the Applet LittleShootApplet Started and then no response when I click the "open file" button.

maschek said...

@anonymous
OS? browser? Java version? please be specific

maschek said...

The class and java files are available again from the mozilla site. As I see it has changed somewhat, so better try the old and the new one if any of them works for you.

This time i mirrored the java file as well:
http://maschek.hu/preview/ffx3_file/LittleShootApplet.java

maschek said...

Ok, to use the _latest_ class from Adam Fisk, you will have to change the function call
document.LittleShootApplet.fileDialog
to
document.LittleShootApplet.openDialog
in my filepath.html.

Thats it.

Anonymous said...

Sorry but i've a problem. I have a site in local whit easy php on. When i click on the upload button anything appens... i've the appletLoader.js file in the folder of the site... Shoud i've to compile the applet?

maschek said...

@Anonymous

Normally you dont have to compile the applet. If you use the code downloaded from my demo site, it should work, if you use the newer class file, you should change the function name as stated above.

Michael said...

I tried to check out the demo, but it didn't work. The site seems to be down. I also tried out Adam Fisk's .class file as an applet, but it didn't show anything....just a white area in FF3. Am I doing something wrong?

I just used this...but no visible function to select the file:
<APPLET CODE="LittleShootApplet.class" WIDTH=200 HEIGHT=200>
</APPLET>

I then embedded the rest as:
<script>
document.LittleShootApplet.fileDialog("onFileDialogFile");

function onFileDialogFile(path)
{
// Converts from a Java string to a JS string.
var javaScriptPath = path + "";
alert (javaScriptPath);
}
</script>

What am I doing wrong?

maschek said...

My website is currently down.
I will try to have it fixed ASAP then i get back to you.

maschek said...

@Michael

The website is up, please check the demo again.

Michael said...

I've tried your demo on IE7 and Firefox with JRE version 1.6.0_05 (on WinXP) and nothing happens when I click on the open file button. Any clue why it is not working?

maschek said...

@Michael

Works fine for me on XP with IE8 in IE7 compatibility mode with Java 1.6.0_10-rc-b28.
Do you have the applet initializing? Do you get a java error message in the explorer status bar? Do you have a java icon showing up in your taskbar?

titemoku said...

hello,

the script works fine on FF and opera but not on ie7 or IE8.
I've got this error when i click on the button:
object doesn-t support this property or method.
do you have an idea?
thanks

Chloe said...

I've tried the demo on IE7 and Firefox3 with JRE version 1.6.0_11-b03 (on WinXP) and nothing happens when I click on the open file button.

The error message shown in Firefox is document.LittleShootApplet.fileDialog is not a function.

And also how to do the applet initializing?

Anonymous said...

The source for the Applet is not available at http://maschek.hu/preview/ffx3_file/LittleShootApplet.java

Sharon said...

Thanks for this.

I tried this applet but for some reason it is not working.

It throws this exception:

uncaught exception: Error calling method on NPObject! [plugin exception: java.security.AccessControlException: access denied (java.io.FilePermission /home/npc25 read)].

So I thought it might be something about a signed applet so I signed the applet and added this line of code into the applet:

archive="LittleShootApplet.jar"

When I run the page, the cert came up and I ran the applet. The applet shows, but the exception error got thrown again, and my firefox freezes.

Any idea why?

maschek said...

@Sharon

Dont really know. Make sure you include the applet from the same domain where your page is.

Anonymous said...

Good fill someone in on and this fill someone in on helped me alot in my college assignement. Gratefulness you as your information.

Anonymous said...

I'm trying this code but it doesn't work

mrmiagi0101 said...

Nice Applet! Works great! And in Java 7 with multible file selection...

But why can't I read the selected file? I have in the
public void openDialog:
....

final String directory = m_fileDialog.getDirectory();
final String returnFile = m_fileDialog.getFile();
final File file = new File(directory+File.separator +returnFile);
try
{
FileInputStream fileInputStream = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
try
{ fileInputStream.read(data);

.....

With this code I get the: java.security.AccessControlException: access denied (java.io.FilePermission ... read)

OK then I signed the applet, but the same error.
After some google searches I found if you want to call from JS to a signed applet, you have to put the
trusted code part in sth. like that:
....

byte[] test = (byte[]) java.security.AccessController.doPrivileged(new java.security.PrivilegedAction()
{
public Object run()
{
try
{
FileInputStream fileInputStream = new FileInputStream(file);
....
But now I get a uncaught exception: java.lang.reflect.InvocationTargetException

So please I need your help how to solve this problem...

maschek said...

@mrmiagi0101
I am afraid I can't help you with this as my Java knowledge is quite limited.

MBarni said...

Source code is missing again... where do I find it?

Adam Maschek said...

@MBarni - Seems like I do not have the original class file. Try contacting Adam Fisk: https://github.com/adamfisk

Lisa Nek said...

My problem of copying and moving of ling path file solved by suing long path tool. I Suggest everyone to try this.