views:

1330

answers:

4

I would like to trigger a click on the browse button on a hidden FileUpload widget. The following code works fine on IE 6+, but doesn't work in FireFox.

final FileUpload upload = new FileUpload(); 

upload.setVisible(false);
upload.setName("uploadFormElement"); 
panel.add(upload);

panel.add( new Button("Select File", new ClickListener()
{ public void onClick(Widget pSender) 
{ jsClickUpload( upload.getElement() ); } }));

native void jsClickUpload( Element pElement ) /*-{ pElement.click(); }-*/;

How can I achieve the same in FireFox (and possibly other browsers)?

A: 

have you tried calling the onClick() method directly?

Nick Josevski
The FileUpload widget in GWT has no click method, so you have to go use inline native javascript to do it.
Drejc
A: 

The solution can be read here:

http://www.quirksmode.org/dom/inputfile.html

in the last paragraph:

The click() method allows you to simulate a click on a form field. Checkboxes get toggled, radios selected, and so on. Unfortunately Mozilla and Opera haven't added this method to file upload fields. I wonder why, adding it is not really a security risk since the worst that can happen is that the file selection window pops up.

Drejc
the file popup is a modal dialog, so it can be used to spam and cause DoS attacks on the user - imagine a dialog u cant close!
Chii
Also, an evil script could trigger a browse dialog at a carefully chosen time to cause the user to send a file somewhere other than where they expected.It's worth noting also, that this is no longer possible in flash as of Flash 10.
Mark Renouf
A: 

The .click() on a FileUpload only works in IE but when you try to submit you get a JavaScriptException.

bimbojones
A: 

The click method is currently in the process of being implemented in FF 4. It is being discussed what security rules will be implemented and it sounds like it will be similar to Window.open and must be the direct result of a user action and not a timer or load.

LINEMAN78