How to invoke Upload from File Open MenuItem?

How can I invoke the Upload widget from a MenuItem?

The Basic MenuBar demo shows how to capture the “File Open” menu item action. The Single-Click Upload demo shows how to pop up a file selection dialog. I would like the menu item action to pop up the file selection dialog. So far I have only been able to accomplish this through a two step process: the file-open menu item pops up the Upload widget, then the user needs to click the “select file” button on the widget. I would like the “select file” button to be invoked immediately through the File-Open menu action.



So perhaps the menu command needs to invoke some javascript directly as attempted below? But it isn’t quite right. Thing is, File - Open File is even part of the basic menubar demo, so I would think there must be a way to do this, but the sample stops before actually doing the action.

    private Command uploadCommand = new Command() {
        public void menuSelected(MenuItem selectedItem) {
            String fileUploadDiv = "<div><form id='fileForm'><input type='file' id='theFileId' name='uploadFile'/></form></div>";
            try {
                CustomLayout cl = new CustomLayout(new ByteArrayInputStream(fileUploadDiv.getBytes()));
            } catch (IOException ex) {
            getWindow().showNotification("Upload Invoked");

Your main problem will be the same is in
- browsers prevent opening the file dialog from XHR responses.

To make the click() javascript hack work, it has to be triggered directly from a user action and not as a consequence of fetching something from the server.

Thank-you for the response. It was really starting to look non-trivial to do this. My conclusion is that I can’t really use a MenuBar to perform a “File - Open” operation. I have to consider laying out the widgets in some other manner.