19

DownloadLink is nice and handy for creating a button/link for downloading a file, along these lines:

add(new DownloadLink("downloadButton", getReportFile(), "report.pdf"));

and

<input type="button" wicket:id="downloadButton" value="Download" />

However, I would like to trigger the generation of the file to download only when the button/link is clicked. In other words, upon click, I'd call a method that generates the file (a Pentaho report in our case), puts it in a temp place and returns a File pointing to it. Then I'd tell the DownloadLink to use that File. Question is, is this possible somehow?

Currently we have something like the code below, which works, but I'm interested in whether DownloadLink could be used instead.

add(new Link<Void>("downloadButton") {
  @Override
  public void onClick() {
    IResourceStream resourceStream = new AbstractResourceStreamWriter() {
      @Override 
      public void write(OutputStream output) {
        try {
          reportService.generateReport(output, report);
        } catch (IOException e) {
          // ...
        }
      }

      @Override
      public String getContentType() {                        
        return CONTENT_TYPE_PDF;
      }
    };

    getRequestCycle()
      .setRequestTarget(new ResourceStreamRequestTarget(resourceStream)
      .setFileName("report.pdf"));
  }
});

(Wicket 1.4.18, if it makes a difference.)

approxiblue
  • 6,982
  • 16
  • 51
  • 59
Jonik
  • 80,077
  • 70
  • 264
  • 372

2 Answers2

15

Can't you use the constructor that takes a Model as argument? And make the Model generate the File in its getObject(). A LoadableDetachableModel is a good choice, given that load(), and therefore file generation, will be invoked only once.

If the file is to be freshly generated every time the link is clicked, use DownloadLink.setDeleteAfterDownload(true) to ensure the File is automatically deleted once it is served.

I don't use 1.4, but the source code in 1.3 shows that the File is retrieved by means of getModelObject() in the onClick() method of the Link.

IModel fileModel = new AbstractReadOnlyModel(){
    public Object getObject() { 
        return generateFile();
    }
};

DownloadLink link = new DownloadLink(linkId, fileModel, "report.pdf");

Source code of DownloadLink.onClick()

public void onClick()
{
    final File file = (File)getModelObject();
            ...
    IResourceStream resourceStream = new FileResourceStream(
            new org.apache.wicket.util.file.File(file));
    getRequestCycle().setRequestTarget(.../* uses resourceStream */...);
}
Xavi López
  • 27,550
  • 11
  • 97
  • 161
  • 2
    You could also override the onClick of DownloadLink and do a `setModelObject()` before calling the super method. Xavi's suggestion is much cleaner, though. – jbrookover Oct 04 '11 at 10:22
  • 4
    1. Use a `LoadableDetachableModel` instead. `getObject()` may be called multiple times during a request, but `load()` won't; 2. if the file is always re-generated, you may call `downloadLink.setDeleteAfterDownload(true)`, so you don't have to worry about deleting the temp file afterwards. – tetsuo Oct 04 '11 at 11:39
  • @tetsuo Thanks for your valuable comments. I've updated the answer with your recommendations. – Xavi López Oct 04 '11 at 11:55
  • Thanks! Got it working this way. Also thanks to @tetsuo for mentioning `setDeleteAfterDownload(true)` which is just what I needed. – Jonik Oct 04 '11 at 16:44
  • 3
    You may need to call `setCacheDuration(Duration.NONE)` on the `DownloadLink` to force the browser to re-request the file and so re-generate the object. – Jim Jan 15 '13 at 18:24
  • @Jim good to know. I used to append `&anticache=System.currentTimeMillis()` to the URL to manually achieve this in Wicket 1.3 (seems like `setCacheDuration()` was introduced in 1.5). – Xavi López Jan 16 '13 at 10:25
11

Use org.apache.wicket.markup.html.link.ResourceLink instead.

martin-g
  • 17,243
  • 2
  • 23
  • 35
  • What is the benefit of using ResourceLink instead of DownloadLink (as [shown by Xavi López](http://stackoverflow.com/questions/7646270/using-wickets-downloadlink-so-that-the-file-is-generated-when-the-link-is-clicke/7646332#7646332))? (Not sure if it's relevant, but note that here the file to download is not a static resource, but a dynamic report that's always generated anew.) – Jonik Oct 04 '11 at 16:39
  • DownloadLink works with java.io.File, while ResourceLink works with IResource. There are several implementations of IResource coming with Wicket distro most of which generate dynamic data. – martin-g Oct 07 '11 at 14:46
  • How to do the same with latest wicket version? – mathan Oct 09 '22 at 12:36
  • 1
    @mathan It should be the same as in the previous version. Please ask a new question in StackOverflow with as many details as possible! – martin-g Oct 10 '22 at 11:21