如何使用Wicket的DownloadLink与即时生成的文件? [英] How to use Wicket's DownloadLink with a file generated on the fly?

查看:112
本文介绍了如何使用Wicket的DownloadLink与即时生成的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DownloadLink 是很好的,方便地创建一个按钮/链接下载文件,沿着这些路线:

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

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

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

但是,我想触发生成文件,只有当按钮/ link被点击。换句话说,点击后,我会调用一个生成文件的方法(在我们的例子中是一个Pentaho报告),把它放在一个临时的地方,并返回一个文件指向给它然后我会告诉 DownloadLink 来使用文件。问题是,是否可能以某种方式

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?

目前我们有类似下面的代码,这是有用的,但我有兴趣是否 DownloadLink 可以是

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,如果有所作为)。

(Wicket 1.4.18, if it makes a difference.)

推荐答案

不能使用以 Model 作为参数的构造函数并使模型在其中生成文件 getObject()。一个 LoadableDetachableModel 是一个不错的选择,因为 load(),因此文件生成将仅被调用一次。

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.

如果每次点击链接时都要生成新文件,请使用 DownloadLink.setDeleteAfterDownload(true) 确保文件被送达后自动删除。

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.

我不使用1.4,但1.3中的源代码显示了文件通过 getModelObject() onClick() code>链接。

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");

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 */...);
}

这篇关于如何使用Wicket的DownloadLink与即时生成的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆