Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

How to invoke Upload from File Open MenuItem?

Glenn Heinze
1 decade ago Aug 01, 2012 2:46pm

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.

Thanks

EDIT

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() {
        
        @Override
        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()));
                getWindow().addComponent(cl);
                getWindow().executeJavaScript("document.getElementById('theFileId').focus().click();");
            } catch (IOException ex) {
            }
            
            getWindow().showNotification("Upload Invoked");
            
        }
        
    };
Last updated on Aug, 2nd 2012
Leif Åstrand
1 decade ago Aug 03, 2012 6:47am
Glenn Heinze
1 decade ago Aug 03, 2012 11:38am