Tim Hill
Tim Hill

Reputation: 31

GWT Programmatically Click FileUpload

Firstly, my development environment is Chrome/Safari with Eclipse Luna and Super Dev Mode on OSX Yosemite.

I am having a bit of a time with this widget. I have created a number of upload widgets (image, audio, video etc) which extend a base upload widget. The image upload calls FileUpload.click() upon initialisation and this works as expected in the the file dialog is shown. However, for the audio and video uploads, I need the user to select an option from a SuggestBox and then once a selection is made, FileUpload.click() should be fired (I already have the event handler in place etc). This last bit is not working, or doesn't appear to be as I do not get the file dialog being shown. I have traced the code execution in SuperDevMode and FileUpload.click() does appear to get fired (execution moves past this point and no error/exception is thrown), but I cannot, for the life of me get the dialog to be shown. The FileUpload element is not hidden/disabled (it used to be 'off screen', but I changed this just to be sure that that wasn't the issue).

What might be causing this - could it be an OS/browser issue, rather than a GWT one?

UPDATE

I decided to add a ClickHandler to the FileUpload widget which simply writes to the console when fired. Through both mechanisms the upload widget is 'clicked', so I really am at a loss of where to go next. Interestingly, if I remove the SuggestBox from the audio upload and just call the click function, I do get the popup, but I don't see why this should interfere with click if present.


Solved

Solved it. So it seems that trying to trigger a FileUpload click programmatically only currently works if the event triggering it is a ClickEvent. To solve this I have had to create my own native click event and fire that when the user makes a selection from the SuggestBox. Is this intended behaviour?

Upvotes: 0

Views: 879

Answers (1)

Michael
Michael

Reputation: 33297

You can trigger the click of a FileUpload like this:

FileUpload f = new FileUpload(); 
f.getElement().<InputElement>cast().click();

For it to work in WebKit-based browsers (Chrome, Safari), the FileUpload has to be "moved out of view" but not hidden (as in setVisible(false), which sets the CSS display property to none), i.e. something like (in CSS): position:absolute; top: -1000px; left: -1000px;

Upvotes: 1

Related Questions