0

Good day all, I would like to know if someone can help me establish the following: I have a grid with a form. Every time I save the form, it creates a new row with the values from the form. What I want now, is a download button on every row, to download the content of the selected row and put it in a text file. So in the end, I'll be able to download a .txt file with contents of the selected row.

I'm using Vaadin 10

Please help.

What I would like to get

document id  attchfilename   download
1             xxx               download
2             yyy               download

This below is what I've already tried, but I would like to do it with a download button.

Anchor anchor = new Anchor(getStreamResource("default.txt", "default content"), "click me to download");
        anchor.getElement().setAttribute("download",true);
        textField.addValueChangeListener(e -> {
            anchor.setHref(getStreamResource(textField.getValue() + ".txt", textField.getValue()));
        });
    public StreamResource getStreamResource(String filename, String content) {
        return new StreamResource(filename,
                () -> new ByteArrayInputStream(content.getBytes()));
    }
  • You seem to have already found out how to do the download part, and all you need now is to display a button instead of a link. Or does the download not work with your anchor solution yet? If it works already, please check out [File Download Wrapper](https://vaadin.com/directory/component/file-download-wrapper/samples) add-on, it allows you to show a button instead of a link. This should solve your problem. – kscherrer May 17 '19 at 07:23
  • @KasparScherrer, a test with the anchor does work, but I want to have the selected row to be downloaded in a .txt file. – Ruben Tjok May 17 '19 at 11:27
  • Ah, then you're gonna have to create a text File, probably in your getStreamResource method, and write the content in it. Then you make a byte array out of that, which you'll use for the ByteArrayInputStream creation. – kscherrer May 17 '19 at 11:37
  • I've tried with the vollowing and it worked `private Anchor createDownloadLink(Grid grid, License item) { Anchor anchor = new Anchor(getStreamResource("default.txt", "default content"), "Download Test"); anchor.getElement().setAttribute("download", true); anchor.setHref(getStreamResource("cbs.lic", item.getTestValue())); return anchor; }` – Ruben Tjok May 17 '19 at 14:00

0 Answers0