Alex
Alex

Reputation: 223

Allow user to download generated XML on Java EE/Wicket

My application (Wicket framework with Java EE) has a wizard-like style and generates a xml file. I would like to offer the user a button, with which the file can be downloaded. How can I offer such a function, preferably without saving the file on a server?

Any help would be really appreciated

Upvotes: 0

Views: 644

Answers (2)

sedden
sedden

Reputation: 91

This example is good point to start: AJAX update and file download in one blow.

Then, the only thing you need to do is to implement the IResourceStream for your generate XML.

public class YourXmlDownload implements Serializable, IResourceStream {


    protected byte[] xmlContent = null;

    // ...

    @Override
    public final Bytes length() {
        return Bytes.bytes(xmlContent.length);
    }

    @Override
    public final InputStream getInputStream() throws ResourceStreamNotFoundException {
        return new ByteArrayInputStream(xmlContent);
    }

}

You could use the ByteArrayInputStream as shown in the example above.

Upvotes: 1

OnesAndZeros
OnesAndZeros

Reputation: 383

I just use an org.apache.wicket.markup.html.link.DownloadLink

HTML:

<input wicket:id="export" type="button">

Java:

File file = new File(appHome + "/template.xlsx");
add(new DownloadLink("export", file, "template.xlsx"));`

I store my file on the server, but here is another question with more info on how to generate your file on the fly: How to use Wicket's DownloadLink with a file generated on the fly?

Upvotes: 1

Related Questions