Search code examples
vaadinhilla

How to create a button for dynamically created file download?


To provide dynamically created file download button in Vaadin, we usually create a Button with Anchor wrapper around and use StreamResource to return file data to browser as file download. How to accomplish that in Hilla? I can't see a Anchor element available in view? I was looking for Vaadin Fusion example but no luck.

Edit 1: Here is a screenshot when accessing SpringBoot controller URL. Hila is actually responding ... enter image description here


Solution

  • Create a REST Controller and use a standard <a href="..."> to download the file.

    Here is an example how this would look to download a PDF:

    @RestController
    @RequestMapping("api/file")
    public class FileController {
    
        @GetMapping(produces = MediaType.APPLICATION_PDF_VALUE)
        public byte[] get(String parameter) {
            // return the file
        }
    }