Niby
Niby

Reputation: 494

Vaadin FileDownloader: start download manually

I am building a project which downloads several PDF files from different URLs, merges them into a single one and downloads it.

I'm trying to use Vaadin's FileDownloader to achieve this:

final FileDownloader fileDownloader = new FileDownloader(new FileResource(resultResource.getFile()));
fileDownloader.extend(download);

The resultResource is the generated PDF which I want to download.

Now the problem is that it takes a short time to generate the PDF, so that sometimes the download happens before the new file is generated, resulting in it downloading the old file, or an empty one.

So what I've been trying to do is something like this:

download.addClickListener(e -> {
    try {
        // This creates the new PDF 
        pdfConverter.manipulatePdf(storeNumber.getValue());

        fileDownloader.download();

    } catch (...) {
        ...
    } 
});

But so far without any success. Is there any way to something like this? To disable the "automatic" download and trigger it manually?

Upvotes: 0

Views: 949

Answers (1)

Tatu Lund
Tatu Lund

Reputation: 10633

There are two approaches you can try

First approach is to refactor your UI so, that PDF file is started to be generated when you enter the view, and once complete you enable the download button. You can have other indicators like progress bar if that is feasible.

In Vaadin 8.4+ you can also setup FileDownloader by extending EventTrigger (see pull request https://github.com/vaadin/framework/pull/10478 ) and API spec https://vaadin.com/download/release/8.4/8.4.2/docs/api/com/vaadin/server/EventTrigger.html That could be something to be exploited if the first way is not applicable for you.

Upvotes: 2

Related Questions