FileDownloader MenuItem not supported?

Helo, good to see the new FileDownloader extension to release. I’ve tried for the link and button for this downloader and they are working fine. Thanks to Vaadin!! But, now i’ve a problem, i had a menu item which enable user to download when clicked. And i try to use the new FileDownloader to extend my menu item but no success. The error is menu item cannot be cast to AbstractComponent. I wonder is the menu item is not support for this extension? Or any way to work around for this ? Cos i dun want to change my menu item to button or link. Any help please?

As for update, i try to extend the FileDownloader to my menuBar which is AbstractComponent, but it end up every click on my menu items will trigger the download to happen, even which is not meant for the download.

The FileDownloader that was added yesterday is currently only working with entire components - e.g. individual menu items are not supported and extending the MenuBar will make the download start by clicking any part of the component, which is typically not the intended behavior.

If the reason you want to use FileDownloader is to avoid problems with popup blockers in some browsers, I’m afraid there are currently no good alternatives if using MenuBar. There are however some workarounds that you could use If the reason is that you can’t use Page.open or Page.setLocation without knowing the URL. If the resource is dynamically generated, you could generate it using a RequestHandler instead of as a Resource and manually constructing the URL based on e.g. Page.getLocation. It it’s just a static file, you could do the same thing but even without using the RequestHandler as long as you put the file in some location that the servlet container automatically handles for you.

I hit this today. Would like to see a fix for this.

Arg. Forgot and hit it again. Downloading based on a menu item selection would be nice.

The scenario is just that I have a number of downloads (file types) that make sense on a particular screen. Using a menu to save space. I could do this with sub-Windows (or something similar), but it’s not very pretty.

Here is my work-around. It works like a charm for me. Hope it will help you.

  1. Create a button and hide it by CSS (not by code: button.setInvisible(false)):

    final Button downloadInvisibleButton = new Button(); downloadInvisibleButton.setId("DownloadButtonId"); downloadInvisibleButton.addStyleName("InvisibleButton");

    In your theme, add this rule to hide the downloadInvisibleButton:
    .InvisibleButton { display: none; }

  2. When the user clicks on menuItem: extend the fileDownloader to the downloadInvisibleButton, then simulate the click on the downloadInvisibleButton by JavaScript:

    [code]
    menuBar.addItem(“Download”, new MenuBar.Command() {
    @Override
    public void menuSelected(MenuBar.MenuItem selectedItem) {
    FileDownloader fileDownloader = new FileDownloader(…);
    fileDownloader.extend(downloadInvisibleButton);

    //Simulate the click on downloadInvisibleButton by JavaScript
    Page.getCurrent().getJavaScript().execute(“document.getElementById(‘DownloadButtonId’).click();”);
    }
    });
    [/code]

Sneaky. I like it!

Good solution.

this is exactly what I looking for. downloading files by code/programmatically!!! (^o^)/

The workaround works, but I’d like to have menu items extendable by file downloader. Any news on that?

I need this feature too! Still don’t works in 7.7.7 … :frowning:

There is an (open) issue here:
https://github.com/vaadin/framework/issues/4057

Definitely I could use this feature too…
In the meantime, that work around works great, thanks Nhat Nam Nguyen!
I wonder why Button.click() doesn’t work instead of Javascript snippet.

That’s because Button.click() does not go client-side - rather it directly calls the Button’s server-side click listeners. The FileDownloader extension needs to be activated client-side (in the browser) in order to work.