如何将 Wicket 的 DownloadLink 与动态生成的文件一起使用?

发布于 2024-12-08 03:52:08 字数 1396 浏览 0 评论 0原文

DownloadLink 对于创建用于下载文件的按钮/链接来说非常好用且方便,沿着这些思路:

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

但是

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

,我想仅在以下情况下触发文件的生成以下载:单击按钮/链接。换句话说,点击后,我将调用一个方法来生成文件(在我们的例子中是 Pentaho 报告),将其放在临时位置并返回一个指向它的 File 。然后我会告诉 DownloadLink 使用该 File。问题是,这是否可能

目前我们有类似下面的代码,它可以工作,但我感兴趣的是是否可以使用 DownloadLink 来代替。

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,如果有影响的话。)

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.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明明#如月 2024-12-15 03:52:08

您不能使用以 Model 作为参数的构造函数吗?并使Model在其getObject()中生成File。 LoadableDetachableModel 是一个不错的选择,因为 load() 以及文件生成只会被调用一次。

如果每次单击链接时都要重新生成文件,请使用 DownloadLink.setDeleteAfterDownload(true) 确保文件一旦被自动删除服务。

我不使用1.4,但1.3中的源代码显示,File是通过onClick()中的getModelObject()检索的> 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 */...);
}

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 */...);
}
友欢 2024-12-15 03:52:08

请改用 org.apache.wicket.markup.html.link.ResourceLink。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文