FileDownloader and window close.

I’ve a modal window ( a popup) in which I download a file.
I used FileDownloader to extend my OK button and it works.
Now I need to add a clickListener to the OK button in order to perform some other actions and close the window.
Everything works fine until I add the window close().
When the close is added to my clickListener the the FileDownloader simply does nothing.
Why ?
How can I avoid this borrowing behaviour ?
Tks

Hey Tullio,

Hmm, interesting. Could you provide a code example? Does the code below work for you?

    @Override
    protected void init(VaadinRequest request) {
        final VerticalLayout layout = new VerticalLayout();
        setContent(layout);

        final Window w = new Window();

        Button downloadButton = new Button("Download image",
                new ClickListener() {
                    @Override
                    public void buttonClick(ClickEvent event) {
                        Notification.show("Hello World!");
                        w.close();
                    }
                });
        StreamResource myResource = createResource();
        FileDownloader fileDownloader = new FileDownloader(myResource);
        fileDownloader.extend(downloadButton);

        w.setContent(downloadButton);
        getUI().getCurrent().addWindow(w);
    }

    private StreamResource createResource() {
        return new StreamResource(new StreamSource() {
            @Override
            public InputStream getStream() {
                String text = "My image";

                BufferedImage bi = new BufferedImage(100, 30,
                        BufferedImage.TYPE_3BYTE_BGR);
                bi.getGraphics().drawChars(text.toCharArray(), 0,
                        text.length(), 10, 20);

                try {
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    ImageIO.write(bi, "png", bos);
                    return new ByteArrayInputStream(bos.toByteArray());
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }

            }
        }, "myImage.png");
    }

No it not working and this is my problem.
Tks

Hi Tullio,

I was having the exact same issue a few weeks ago (see https://vaadin.com/forum#!/thread/8123325) and have just stumbled upon something that might help.

I presume you are using firefox as from what I have seen the file downloads as expected when using Chrome or IE.

I found that adding in a small sleep in the click listener before the window is closed was enough to get the file downloading correctly in Firefox. However I don’t know what factors influence how long the code would need to wait before closing the window though.

Hopefully this should help.

Hello all,

in our use case we had a button which triggers both download and window close. We had to add two separate delays to window close: 1. button click listener starts a client side timer. 2. When timer expires and event is fired to server side there is small delay in before window is closed.
With this setup the download button is not removed too early.

same problem here… I’ve tried to add some sleep into click listener which closes the window, but it doesn’t seem to work 100% of the time… sometimes Window isn’t closed, sometimes download doesn’t trigger…